Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

itoa and string variables

This topic is 4807 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 want to convert a number to a string itoa works with char*s and it is kind of a pain to convert everything from char*s to strings. Is there a better way to do it?

Share this post


Link to post
Share on other sites
Advertisement
Hi,

You can do the following i believe,

std::string myString = "15";

int ret = itoa( &myString[0] );

Give that a go.

ace

Share this post


Link to post
Share on other sites
Don't use itoa(), it is not ANSI, and not implemented on all the platforms.
Use sprintf
somethign like sprintf(str,"%i",number);

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
std::string myString = "15";
int ret = itoa( &myString[0] );


That's atoi, and the correct way with a std::string would be atoi( myString.c_str() );

If you want itoa (integer->ascii) and using std::strings try using std::ostringstream

std::ostringstream ss;
ss << num;
std::string theStr = ss.str();

Share this post


Link to post
Share on other sites
Not sure why the conversion to string from char* is such a pain, but you really should be doing it in one of the suggested ways anyway - preferably with boost::lexical_cast, which is a general-purpose wrapper around the stringstream manipulation that will check for a few weird corner cases that come up when you try to make it really general :)

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!