• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: A question I'm ashamed to ask
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A question I'm ashamed to ask


  • Subject: Re: A question I'm ashamed to ask
  • From: Sherm Pendley <email@hidden>
  • Date: Wed, 20 Oct 2004 14:51:39 -0400

On Oct 20, 2004, at 1:48 PM, April Gendill wrote:

Ok. I work with plain old ansi c so little that this has escaped me. In one of my application I need to take a plain old C character array
and make it into a string but the string needs to use a conversion character.

so I basically I need
char * something = "%x", someInteger;

basically I need to convert the integer into it's hex value and place it in the string.
I can't figure out how to do this.

The simplest "pure C" solution is the sprintf() function - but as with many plain ol' C tasks, there are potential hidden gotchas - you need to allocate the buffer that something points to yourself, it can't point to a constant string, and it must be large enough to hold the hex representation of someInteger.

So, assuming that someInteger is a 32-bit int, you'll need 8 hex digits to hold it, and a terminating NULL. Just to be paranoid, we'll allocate a bit more than that.

char *something = malloc(16);
sprintf(something, "%x", someInteger);

There's also a snprintf() function, that additionally prevents buffer overruns by truncating the output to a maximum size if necessary:

char *something = malloc(16);
snprintf(something, 16, "%x", someInteger);

Because you've used malloc() to create your buffer, you'll need to use free() at some point to release it:

free(something);

Or, you could take advantage of Objective-C's built-in memory management to create an autoreleased buffer:

char *something = (char*)[[NSMutableData dataWithCapacity:16] mutableBytes];
snprintf(something, 16, "%x", someInteger);

Or, for that matter, you could use NSString's formatting constructor:

char *something = [[NSString stringWithFormat:"%x", someInteger] UTF8String];

This last option is the slowest - it transforms the string twice, first from an int into NSString internal representation, and then from that into a C string - but for such a small string that won't matter unless you're doing it millions of times. And, it's easier for an Objective-C developer who's unfamiliar with the C functions to understand and maintain. So I'd go with the NSString method, and replace it with one of the "pure C" methods if and only if Shark showed it to actually be a bottleneck.

sherm--

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: A question I'm ashamed to ask
      • From: Basil Achermann <email@hidden>
References: 
 >A question I'm ashamed to ask (From: April Gendill <email@hidden>)

  • Prev by Date: Re: A question I'm ashamed to ask
  • Next by Date: [Newbie] Looking for local newbies
  • Previous by thread: Re: A question I'm ashamed to ask
  • Next by thread: Re: A question I'm ashamed to ask
  • Index(es):
    • Date
    • Thread