Does anybody know of any standard C++ functions to convert an int to a char*?
Something that would take the number 9876 to {'9','8','7','6'}?
I would rather not reinvent the wheel if there is something like this already.
Thanks,
Brian Budge
Printable View
Does anybody know of any standard C++ functions to convert an int to a char*?
Something that would take the number 9876 to {'9','8','7','6'}?
I would rather not reinvent the wheel if there is something like this already.
Thanks,
Brian Budge
u can use itoa();
This isn't C++ specific, but...
int integer=9876;
char string[20]; // be big enough for the digits
sprintf(string,"%d",integer);
// string now contains "9876"
The original poster was asking for a standard C++ function to convert an int to a char*, and _itoa is not standard, it is Microsoft specific. Some other compilers may support it, but it is not part of the standard library. If Brian Budge is only interseted in the Windows platform, itoa is fine, but it is not standard.
The more portable way to do this is as Eric Smith said, sprintf.
HTH
--michael
yes .. its true.. i anwsered too fast..
You can do:
char *str;
int nValor=9876;
str = new char ;
wsprintf(str,"%d",nValor);
Hope this help.
Leila
you wouldn't want to do that. You are only allocating 1 byte, and then trying to write a 5 byte string into it. you would have to new a char array big enough to hold all the numbers.
str = new char [5]; // atleast 5.
and then delete it later. Of course if the length is constant
char str[5]
is better.
HTH
--michael