Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JoHnOnIzEr

Integer to String

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

How can i turn an integer into a string? Like if int blah = 5, i want to be able to stick that 5 into a string and not turn it into a char. Anyone understand what im saying and know how to do this?

Share this post


Link to post
Share on other sites
Advertisement
What programming language are you using? Assuming C++, I would just use boost::lexical_cast. You can achieve the same effect manually with a std::stringstream. In C you''d probably use sprintf().

Share this post


Link to post
Share on other sites
int blah = 5;
string s;
s = blah;

Search the forum or google. Questions like this come up around every 5 minutes.

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
This is not a game, so it must be homework.

How do you figure? I haven''t played many games that didn''t use strings in one form or another.

Share this post


Link to post
Share on other sites
quote:
Original post by JoHnOnIzEr
I need to pass it to a function though as a char ><. And mathematix, i dont take a programming course


Again, google. I can''t see it taking you more than 5 straight seconds to find an answer to this.

Look up:
string::c_str()

Share this post


Link to post
Share on other sites

// needed stl headers
#pragma warning(disable:4786)
#include <sstream>
#include <string>

// variables
std::stringstream ss;
std::string MyString;
int blah = 5;

// convert from int to string
ss << blah;
ss >> MyString;

// get char* array
char const* pszMyString = MyString.c_str();

Share this post


Link to post
Share on other sites
quote:
Original post by glassJAw
int blah = 5;
string s;
s = blah;



This doesn''t do what you think it does. This creates a string where the first character is equal to a non-printing ascii character. s will not equal "5".

Share this post


Link to post
Share on other sites
whoa guys. How about itoa()?

Here''s the prototype:
char * itoa ( int value, char * buffer, int radix );

radix is the base number system you want to display it in. 99% of the time, it''s 10.

More info here.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!