Jump to content
  • Advertisement
Sign in to follow this  
Tertsi

Any convenient ways of converting ints to strings?

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

Are there any convenient ways of converting ints to strings? Without having to do huge if ladders or alike.

Share this post


Link to post
Share on other sites
Advertisement
Boost lexical_cast, or for a basic mockup of what it does:

template<typename R, typename T>
R lexical_cast(const T& t) {
R ret;
std::stringstream st;
if(!(st<<t) || !(st>>ret))
throw std::bad_cast();
return ret;
}

//usage:
std::string s = lexical_cast<std::string>(10);

Share this post


Link to post
Share on other sites
I normally use itoa, but after seeing the way you do it Evil Steve, I think I'm going to change my ways ;-)
What are the params for it though?

Edit: Do you have a link that tells me how to use this fully? I've had a look on msdn, and it's confusing and not very helpful (doesn't give a list of possible params)

Share this post


Link to post
Share on other sites
eh, I'm not sure what you're asking Tera, but the Evil's function is ready for usage.

ex.
int month = 10;
string tempmonth;
tempmonth = IntToString(month);

Share this post


Link to post
Share on other sites
Quote:
Original post by Tera_Dragon
Ok, maybe I didn't word that well.
What I wanted to know was what does the "%d" mean? And what other flags are there?


%d - insert int
%f - insert float
%x - insert int in hex form
%s - insert string

there are some more but i cant remember them

Share this post


Link to post
Share on other sites
%d reads a decimal integer
%i reads an integer
%c reads a char
%f reads a float
%s reads a string
%u reads an unsigned int
etc.

You should find the rest of the flags easily by searching for sprintf reference.

edit: damn mike, was just typing. ;P

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!