Jump to content
  • Advertisement
Sign in to follow this  
biscuitz

convert int to string

This topic is 4349 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

Advertisement
std::string str = itoa(2342348);

Alternatively you can use atoi to convert back. I believe there are other C methods like atof and ftoa (f = float, i = int, a = *char .. XtoY())

Share this post


Link to post
Share on other sites
itoa() isn't a standard C or C++ function, so there's no guarantee that a given C++ compiler will have it. Even if a C++ compiler has it, then there's no guarantee that the function will have the same function signature as the version you posted (most won't). On the compilers that do have a function signature that matches the version you just used, the code you posted may leak memory, as the return value will need to be free()'d.

itoa: just say no.

Share this post


Link to post
Share on other sites
Well, some do. If they do then take advantage of it I say.

Also, because the net here is so slow it took a while to come up with an example. (I don't use itoa that often, if ever)
char buffer [33];
std::string str = itoa (41515,buffer,10);

10 is the base, so if you wanted Hex you'd use 16 or binary would be base 2.


SiCrane: Yeah, I knew that would give a leak. That's why I searched for an example. This one shouldn't give a memeory leak.
Also boost isn't standard C\C++ either, so were on the same page. [grin]

Share this post


Link to post
Share on other sites
:S I tried using itoa with TextOut() and comes up with a bunch of characters on the end (which I don't want to show up) Ima try std strings.

Share this post


Link to post
Share on other sites
Quote:
Original post by PumpkinPieman
Also boost isn't standard C\C++ either, so were on the same page. [grin]


There's only one boost::lexical_cast. There are at least eight different versions of itoa() out there. That's hardly the same page.

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!