Integer to String

Started by
18 comments, last by JoHnOnIzEr 20 years, 3 months ago
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?
Sidra Headquarters
Game engine is currently at version 0.9.8! Currently Down
Advertisement
This is not a game, so it must be homework.
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().
int blah = 5;
string s;
s = blah;

Search the forum or google. Questions like this come up around every 5 minutes.
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.
I need to pass it to a function though as a char ><. And mathematix, i dont take a programming course
Sidra Headquarters
Game engine is currently at version 0.9.8! Currently Down
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()
// needed stl headers#pragma warning(disable:4786)#include <sstream>#include <string>// variablesstd::stringstream ss;std::string MyString;int blah = 5;// convert from int to stringss << blah;ss >> MyString;// get char* arraychar const* pszMyString = MyString.c_str(); 
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".
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.

This topic is closed to new replies.

Advertisement