Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
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.
 

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
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

macrumors 65816
Jul 23, 2004
1,065
0
Dallas
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

Moderator emeritus
Mar 10, 2004
14,424
1,065
Bergen, Norway
widgetman said:
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:
Code:
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... :eek:)
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
39,786
7,518
Los Angeles
Would this work, using the standard C library?
Code:
int myint ;
char mystringbuffer[20] ;

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

hcuar

macrumors 65816
Jul 23, 2004
1,065
0
Dallas
Doctor Q said:
Would this work, using the standard C library?
Code:
int myint ;
char mystringbuffer[20] ;

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

I still like sprintf the best. Personal preference though.
 

hcuar

macrumors 65816
Jul 23, 2004
1,065
0
Dallas
Mitthrawnuruodo said:
Found this on the net:
Code:
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... :eek:)


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

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,424
1,065
Bergen, Norway
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()...

Code:
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

macrumors 65816
Jul 23, 2004
1,065
0
Dallas
Mitthrawnuruodo said:
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()...

Code:
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

Moderator emeritus
Mar 10, 2004
14,424
1,065
Bergen, Norway
hcuar said:
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
Yes, 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... :eek: ;)
 

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
you are forgiven. thank you again for helping me out with my lame n00b questions. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.