Int To String (char*)

Started by
13 comments, last by Anonymous0 22 years, 2 months ago
Hey, Does anyone know of a lazy function to convert an integer in to a Char* in C/C++ ? Anonymous
Advertisement
#include <stdlib.h>

char *itoa(int value, char *string, int radix);

should work

Nimsay
Nimsay
itoa(int, char*, int)

first int is the int to convert to char.
char* is the buffer to put the converted string into.
the second int is the number base to convert the int from. Base 10 for stanard numbers. Base 2 for binary, base 8 for oct, base 16 for hex.
Thanks,

could you let me know what ''radix'' is used for?
Thanks, you beat me to it
Radix is what base you want your number to be viewed as.

IE, base 10 is decimal, 16 is hexidecimal, 8 is octal.


- Houdini
- Houdini
radix is the number base.

radix = 2; would for binary numbers. base 2.
radix = 10; would be for decimal numbers. base 10.
radix = 16; would be for hexadecimal numbers. base 16.

etc. etc.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Just for further info, there is a whole slew of conversion functions that follow a standard format.

atoi is basically a char to int function (a is for char)


There''s also an itoa function (int to char)
atof function (char to float)
ftoa function (float to char)
l for long
d for double
etc.
you can also use:

char szNumber[16];
int nNumber = 1234567890;
sprintf(szNumber,"%d",nNumber);

and actualy, "a" stands for ASCII



Edited by - kwizatz on January 29, 2002 1:21:02 PM
using snprintf is safer than sprintf (which should be banned IMHO).

Watch those buffer overflows people, that''s how security holes are created ! Learn to do it right from the beginning.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement