Re: A question I'm ashamed to ask
Re: A question I'm ashamed to ask
- Subject: Re: A question I'm ashamed to ask
- From: "E. Wing" <email@hidden>
- Date: Wed, 20 Oct 2004 13:57:18 -0700
>
From: April Gendill
>
Subject: A question I'm ashamed to ask
>
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.
>
>
April.
sprintf (or sprintf) is what you generally want. Below is a little
sample program that demonstrates . It's pretty straight forward, but
the tricky
part is the memory management. You need to be sure your buffers are
large enough to hold the string or you risk the typical risks of
dynamic memory (strange behavior, crashes, security exploits, etc).
The man page also shows an asprintf which would remove the memory
burden from you. However this function is new to me.
I'm not sure why you can't use Objective-C/Cocoa for this. I'll
presume you have portability concerns. If this is the case, here are
the portability issues:
sprintf is the most portable, (C89) but snprintf is much safer. I
think snprintf is C99 though a lot of compilers have it. I think I
remember running into some older Microsoft compilers that didn't have
snprintf, so you'll need to check availability on the compilers you
care about. I'm guessing asprintf probably won't show up on a lot of
other compilers.
If you can use C++, you can also look into std::stringstream. It takes
away some of the memory burdens as well. But it's pretty strange
looking stuff to people not familiar with the STL and streams. There
are also a lot of compilers out there with broken stream
implementations.
-Eric
#include <stdio.h> /* For sprintf */
#include <stdlib.h> /* For malloc */
int main()
{
/* Be careful to allocate enough memory */
char buffer[10];
char short_buffer[3]; /* An example of too little memory */
char* dynamic_buffer; /* An example of dynamic memory */
dynamic_buffer = (char*)malloc(9 * sizeof(char) );
if(NULL == dynamic_buffer)
{
printf("Could not allocate memory\n");
return 1;
}
/* Works, but dangerous if your number string is too long */
sprintf(buffer, "%x", 65534);
/* This is safer, Note that you may use sizeof(short_buffer) instead
* of an explicit 3 here. (Don't do this with the dynamic_buffer.)
*/
snprintf(short_buffer, sizeof(short_buffer), "%x", 65534);
/* Same thing as above, but do capital HEX.
* You may not use sizeof(dynamic_buffer) here because the
* sizeof() will return the size of the pointer (usually 4)
* instead of the amount of memory you allocated.
*/
snprintf(dynamic_buffer, 9, "%X", 65534);
/* Should print fffe */
printf("%s\n", buffer);
/* Should print ff */
printf("%s\n", short_buffer);
/* Should print FFFE */
printf("%s\n", dynamic_buffer);
/* Don't forget to free dynamically allocated memory */
free(dynamic_buffer);
return 0;
}
_______________________________________________
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