Jump to content
  • Advertisement
Sign in to follow this  
MikeDee

int to *char [C++]

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

It's a pretty straitforward question, how do I convert a int to a char* or string (in C++) ? I googled but amazingly I couldn't find anything relevant.

Share this post


Link to post
Share on other sites
Advertisement
Your google-fu needs some work. "int to std::string C++" turns up hundreds of thousands of results.


Anyway, the solution is,

#include <sstream>

int i = 42;
std::string s;
std::stringstream out;
out << i;
s = out.str();





Or you can use C's itoa() function.

Share this post


Link to post
Share on other sites

#include <sstream>

int main()
{
int n = 42;
std::string str;

std::ostringstream ss;
ss << n;
str = ss.str();

return 0;
}




EDIT: Bah, too slow

[Edited by - Evil Steve on August 19, 2009 7:37:00 AM]

Share this post


Link to post
Share on other sites
ahah thanks guies, it solved my problem.

choffstein, I tried searching "int to std::string C++" and google only came up with two results. :P

Thanks a bunch anyway ! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeDee
ahah thanks guies, it solved my problem.

choffstein, I tried searching "int to std::string C++" and google only came up with two results. :P

Thanks a bunch anyway ! :)


Also note that "int to char*" is a bit smeary if you don't intend to take the value of an integer as an address of type pointer to char 0, but to convert it to a string.


0 this was actually my first thought, the solution would have involved type punning or so, leading you into the completely false direction. Luckily I scrolled down first ;)

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!