Converting int to const char*?
I need to know how I could convert an int to a const char*. I am making a game engine and I want to put the Maps x and y positions on the screen while the game is running. The maps position is in an int but the textout function needs const char*. Any help would be very much appreciated.
Assuming C++, you can either use boost::lexical_cast or a std::stringstream to get a string that you can access with c_str().
std::stringstream sstr;sstr << my_int;const char * ptr1 = sstr.str().c_str();std::string str = boost::lexical_cast<std::string>(my_int);const char * ptr2 = str.c_str();
If you care about portable code I highly recommend avoiding itoa(). It is severly non-standard. Not only is it not part of the C standard library, but in non-standard implementations I've seen at least five different function signatures for itoa(), with different memory management conventions:
char * itoa(int, char *, int); // you supply the bufferchar * itoa(int, int, char *); // you supply the bufferchar * itoa(int, char *); // you supply the bufferchar * itoa(int, int); // you need to free the pointerchar * itoa(int); // you need to free the pointer
Quote:Original post by kelcharge
Would itoa() be a route to go?
Better to use strtod, strtol, strtoul. Not only do these calls support specifying the end of the string as a pointer but the strtol and strtoul support different base types so you can convert hex, octal, binary, etc.
assign the int into a char, then cast that to a char *
int i = 124;
char c = i;
char *s = (char *)c;
sounds like you want to do some ASCII backgrounds? if so you only need 1 character per (x,y) coordinate right?
int i = 124;
char c = i;
char *s = (char *)c;
sounds like you want to do some ASCII backgrounds? if so you only need 1 character per (x,y) coordinate right?
Quote:Original post by Interesting Dave
assign the int into a char, then cast that to a char *
int i = 124;
char c = i;
char *s = (char *)c;
Erm, whaaat?
Forgive me but I'm speechless.
no, im pretty sure he jsut want to output corrdinants. what you were sugesting would not work, as char is still a number, so putting an int in it (like 124) would result in the char printing whatever character is 124 in ascii.
you could always make a function that separates an int into units (1's, 10's, 100's, etc) using division and such, then convert those numbers to characters by adding the appropriate ascii value (i cant remember what it is, i think its around 90) this would probably be easiest using recursion. of course, i highly doubt the method the best,or fastest way (actually, its probably in the area of worst, slowest, and most memory consuming)
you could always make a function that separates an int into units (1's, 10's, 100's, etc) using division and such, then convert those numbers to characters by adding the appropriate ascii value (i cant remember what it is, i think its around 90) this would probably be easiest using recursion. of course, i highly doubt the method the best,or fastest way (actually, its probably in the area of worst, slowest, and most memory consuming)
why not just print it to a string?!
int i = 9;
char* str = new char[2];
sprintf(str, "%i", i);
then pass to the function.
function((const char*)str);
int i = 9;
char* str = new char[2];
sprintf(str, "%i", i);
then pass to the function.
function((const char*)str);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement