What am I looking for in the documentation?

  • I have need to turn a local float value into a char array.

    That is, The Quartz 2D graphics function requires the passing of a (const char *) to a text string or I was thinking of a character array.

    My mind is mush at the moment - can't seem to recall the way to program a conversion of

    float fv = 40.0;

    into

    const char *text = "40.0";

    or

    const char text[ ] = "40.0";

    Please advise...

    Thanks YT
  • You might want to look at sprintf <http://linux.die.net/man/3/sprintf>.
    sprintf(text, "%f", fv) should work just fine.

    On Sat, May 4, 2013 at 4:20 PM, YT <yt...> wrote:

    > I have need to turn a local float value into a char array.
    >
    > That is, The Quartz 2D graphics function requires the passing of a (const
    > char *) to a text string or I was thinking of a character array.
    >
    > My mind is mush at the moment - can't seem to recall the way to program a
    > conversion of
    >
    > float fv = 40.0;
    >
    > into
    >
    > const char *text = "40.0";
    >
    > or
    >
    > const char text[ ] = "40.0";
    >
    >
    > Please advise...
    >
    > Thanks YT
    >
  • You need to do a string conversion, just like you would in any situation where you need a textual version of a number.

    One possible way:

    NSString* s = [NSString stringWithFormat: @"%.1f", fv];
    const char* sp = s.UTF8String;

    another:

    char* s[16];
    snprintf(s, 16, "%.1f", fv);

    On May 4, 2013, at 16:20 , YT <yt...> wrote:

    > I have need to turn a local float value into a char array.
    >
    > That is, The Quartz 2D graphics function requires the passing of a (const char *) to a text string or I was thinking of a character array.
    >
    > My mind is mush at the moment - can't seem to recall the way to program a conversion of
    >
    > float fv = 40.0;
    >
    > into
    >
    > const char *text = "40.0";
    >
    > or
    >
    > const char text[ ] = "40.0";
    >
    >
    > Please advise...
    >
    > Thanks YT

    --
    Rick
  • Thanks!

    sprintf worked great - Didn't realize that those wonderful Standard C functions were available.

    Again, Thanks

    YT

    On May 4, 2013, at 4:23 PM, Mohit Sharma <mohit.sharma0690...> wrote:

    > You might want to look at sprintf.  sprintf(text, "%f", fv) should work just fine.
    >
    >
    > On Sat, May 4, 2013 at 4:20 PM, YT <yt...> wrote:
    > I have need to turn a local float value into a char array.
    >
    > That is, The Quartz 2D graphics function requires the passing of a (const char *) to a text string or I was thinking of a character array.
    >
    > My mind is mush at the moment - can't seem to recall the way to program a conversion of
    >
    > float fv = 40.0;
    >
    > into
    >
    > const char *text = "40.0";
    >
    > or
    >
    > const char text[ ] = "40.0";
    >
    >
    > Please advise...
    >
    > Thanks YT
    >
  • On Sat, 4 May 2013 16:20:14 -0700, YT said:

    > I have need to turn a local float value into a char array.
    >
    > That is, The Quartz 2D graphics function requires the passing of a
    > (const char *) to a text string or I was thinking of a character array.
    >
    > My mind is mush at the moment - can't seem to recall the way to program
    > a conversion of
    >
    > float fv = 40.0;
    >
    > into
    >
    > const char *text = "40.0";
    >
    > or
    >
    > const char text[ ] = "40.0";

    Also consider localisation... do you absolutely want the '.' character, or whatever decimal character is specified by the user's locale (or the user himself)?  Consider NSNumberFormatter if you want localisation.

    Cheers,

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • On May 6, 2013, at 09:03 , Sean McBride <sean...> wrote:

    > Also consider localisation... do you absolutely want the '.' character, or whatever decimal character is specified by the user's locale (or the user himself)?  Consider NSNumberFormatter if you want localisation.

    Yes! I don't know what I was thinking with my original suggestion. Sean is right. Use an NSNumberFormatter.

    --
    Rick
  • On May 4, 2013, at 4:20 PM, YT <yt...> wrote:

    > I have need to turn a local float value into a char array.
    >
    > That is, The Quartz 2D graphics function requires the passing of a (const char *) to a text string or I was thinking of a character array.

    Knowing which API you mean, I would recommend against using it (basically Quartz is bad at drawing strings, and you should use a higher level library to do that).

    NSString has simple methods available on both OS X and iOS for drawing strings into the current graphics context, I would highly recommend them instead.
    --
    David Duncan
  • It should go without saying that the "text" char array you suggest must already exist, and be at least one char longer than the longest string you expect (I'd make it twice as long).

    And use snprintf(3) instead. Bad People (or you) will try to overrun the buffer if you don't prevent it. (Not likely with a %f format spec, but even if you trust the dealer, you always cut the cards.)

    — F

    On 4 May 2013, at 6:23 PM, Mohit Sharma <mohit.sharma0690...> wrote:

    > You might want to look at sprintf <http://linux.die.net/man/3/sprintf>.
    > sprintf(text, "%f", fv) should work just fine.
    >
    >
    > On Sat, May 4, 2013 at 4:20 PM, YT <yt...> wrote:
    >
    >> I have need to turn a local float value into a char array.
    >>
    >> That is, The Quartz 2D graphics function requires the passing of a (const
    >> char *) to a text string or I was thinking of a character array.

    --
    Fritz Anderson
    Xcode 4 Unleashed: 4.5 supplement for free!
    http://www.informit.com/store/xcode-4-unleashed-9780672333279
previous month may 2013 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today