Advertisement

How do you set a string = to a int?

Started by March 18, 2000 08:45 PM
10 comments, last by Possibility 24 years, 7 months ago
Lets say i have: char a[]; int b = 12345; so how do I set the string a to "12345" like a = b; and then a would be "12345" Possibility
You could use sprintf, but you need to allocate the memory for the string. so

char a[20];
int b = 12345;
sprintf(a, "%d", b);

would make a be "12345"

Or in C++ you could use stringstreams instead of char arrays.

stringstream a;
int b = 12345;
a << b;
Then a.str() would be "12345"
Advertisement
Assuming that you''re using C, you can''t just say
int a[];

You need to give it a size. For a number, something like
int a[40];
would be plenty. It really depends on how many digits you plan on having.

Anyway, if you have variable b with the value 12345 and you want a to be the string "12345" you can say
itoa(b, a, 10);

This isn''t a standard function however, so it might not be supported by all compilers. I know it works with DJGPP which is what I used to use it in. The first two parameters should be pretty self explanatory, but the third is the radix of the number. (in this case, base 10)
int atoi(const char *s);
char *itoa(int value, char *string, int radix);

These two functions convert int to string and back (itoa = int to string, atoi = string to int), here''s how to use them

int x = atoi("12345");

char buf[5];
char* str = itoa(12345, buf, 10);

Hope that helps, there''s probably other ways, but I don''t know them =)
Chris
quote: Original post by BraveZeus
char buf[5];
char* str = itoa(12345, buf, 10);

Not to be nit-picking, but you forgot to leave room for the null-terminator. You need at least 6 char''s in order to store the string "12345".
haha, good point, don''t know why it works with just 5 then....hmmm....not trying to downplay what you said, because it makes sense that you need the extra character, but it works for me the way it is....do you know why that is?
Chris
Advertisement
in VB this would be soooo easy: a = val(b)
but anyways, just use something like a = (int)b; .
i forgot the term for the int in parenthesis, but i know it works.
The sprintf and itoa methods worked great, thanks guys.

I was already familar with aoti (char to int) and aotf (char to float), so I guessed that ftoa would then do float to char like itoa does int to char. Which leads me to my next question, that didnt work, and i also need to convert a float to a char like:
float a = 123.89;
char b[7] = "123.89"

My C++ programming book covers the aotf and aoti methods, but has nothing at all on itoa, and ftoa does not exist. You guys got any more suggestions?

Possibility
use sprintf();

also try looking on msdn for the function, i bet there is one.
___________________________Freeware development:ruinedsoft.com
quote: Original post by BraveZeus
haha, good point, don''t know why it works with just 5 then....hmmm....not trying to downplay what you said, because it makes sense that you need the extra character, but it works for me the way it is....do you know why that is?

If you compiled in debug mode, the area around the char array might be initialized to zero (specifically the region directly following the array) which will act like a null terminator. It likes to allocated data in multiples of DWORDS, so there''s actually 8 bytes in your char array.

itoa() is a non-standard (non-ANSI) function so it won''t be covered in normal C/C++ books.

This topic is closed to new replies.

Advertisement