Jump to content
  • Advertisement
Sign in to follow this  
kelcharge

Converting int to const char*?

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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();

Share this post


Link to post
Share on other sites
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 buffer
char * itoa(int, int, char *); // you supply the buffer
char * itoa(int, char *); // you supply the buffer
char * itoa(int, int); // you need to free the pointer
char * itoa(int); // you need to free the pointer

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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)

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!