PDA

View Full Version : convert int to char in c




widgetman
Mar 25, 2005, 06:04 PM
Is there a way to convert an integer into a c string? I am not using carbon so none of the cfstr functions will help at all. thanks.



Mitthrawnuruodo
Mar 25, 2005, 06:50 PM
Do you mean an Interger or an int?

And do you need to save the string or can you just printf() it...?

widgetman
Mar 25, 2005, 07:20 PM
I mean an int. i need to use it as a c string and i wont be printing it out at all. is there a way to do something like cocoa's [NSString stringWithFormat:] and just use %i for the ints?

hcuar
Mar 25, 2005, 07:26 PM
Use sprintf... I believe the syntax goes something like this:

char buffer[100];
int myInteger = 10;
sprintf(buffer, "%i", myInteger);

buffer is a character array, the stuff in the quotes is your string formatting, and myInteger is the integer to format as a string.

So... if you did the following code:

cout << buffer << endl;

You'd see:
10

Mitthrawnuruodo
Mar 25, 2005, 07:26 PM
I mean an int. i need to use it as a c string and i wont be printing it out at all. is there a way to do something like cocoa's [NSString stringWithFormat:] and just use %i for the ints?Found this on the net:char * intToString(int num)

{

int i=0;

int j=0;

int k=0;

int ones=0;

char temp[5]; //5=num digits in 32676

char ans[5];



while (num!=0)

{

ones=num%10; //get current ones digit

temp[i]=(char)(ones+48); //48=(int)'0';

num=num/10; //remove current ones digit

i++; //length of number

}

for(j=i-1;j>=0;j--)

{

ans[k]=temp[j]; //reorder string correctly

k++;

}

ans[i]='\0'; //add null char for end of string

return (char *)ans;

}

..but there is a method, I just got to remember what it was called... (it's been a couple of years since I programmed much in c... :o)

Doctor Q
Mar 25, 2005, 07:27 PM
Would this work, using the standard C library?
int myint ;
char mystringbuffer[20] ;

snprintf(mystringbuffer,sizeof(mystringbuffer),"%d",myint) ;

hcuar
Mar 25, 2005, 07:33 PM
Would this work, using the standard C library?
int myint ;
char mystringbuffer[20] ;

snprintf(mystringbuffer,sizeof(mystringbuffer),"%d",myint) ;

I still like sprintf the best. Personal preference though.

hcuar
Mar 25, 2005, 07:34 PM
Found this on the net:char * intToString(int num)

{

int i=0;

int j=0;

int k=0;

int ones=0;

char temp[5]; //5=num digits in 32676

char ans[5];



while (num!=0)

{

ones=num%10; //get current ones digit

temp[i]=(char)(ones+48); //48=(int)'0';

num=num/10; //remove current ones digit

i++; //length of number

}

for(j=i-1;j>=0;j--)

{

ans[k]=temp[j]; //reorder string correctly

k++;

}

ans[i]='\0'; //add null char for end of string

return (char *)ans;

}

..but there is a method, I just got to remember what it was called... (it's been a couple of years since I programmed much in c... :o)


NOOOOOOOOOOOOO!!!!!!!!!!!!!! This is way too complicated. :eek:

widgetman
Mar 25, 2005, 07:35 PM
Thanks for the quick responses everyone!

Mitthrawnuruodo
Mar 25, 2005, 07:35 PM
I'm so stupid (forgive me, its 2:30AM, here...) EDIT: And I see this has been pointed out already... by several posters... :D

You can of course use sprintf()...

int num;
char myBuf[20];
sprintf(myBuf, "%i\0", num);

And when you have it in a char buffer, getting it into a string should be easy, right... ;)

Edit2: Funny that it 20 digits seems to be enough, right Dr. Q...? ;)

hcuar
Mar 25, 2005, 07:38 PM
I'm so stupid (forgive me, its 2:30AM, here...) EDIT: And I see this has been pointed out already... :D

You can of course use sprintf()...

int num;
char myBuf[20];
sprintf(myBuf, "%i\0", num);

And when you have it in a char buffer, getting it into a string should be easy, right... ;)

You don't need the \0 in the format... sprintf automatically adds the null termination...

BTW... not stupid... just took the long way around. :D

Mitthrawnuruodo
Mar 25, 2005, 07:44 PM
You don't need the \0 in the format... sprintf automatically adds the null termination...

BTW... not stupid... just took the long way around. :DYes, I know sprintf() does this for me, but if I've learned one thing from my C/C++ days, it's that it never hurts to terminate the strings manually, too... ;)

And the real irony, is that i suggested printf() in my first respons, without remembering sprintf()... that's really embarrassing... :o ;)

widgetman
Mar 25, 2005, 09:14 PM
you are forgiven. thank you again for helping me out with my lame n00b questions. :)