In order to convert an int (or any other numeric type, e.g., float, double, etc.) to string, you can use:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
Other C/C++ not so frequently asked questions.
In order to convert an int (or any other numeric type, e.g., float, double, etc.) to string, you can use:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
Other C/C++ not so frequently asked questions.
October 13, 2006 at 4:13 pm |
Here’s a more generic version:
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
July 3, 2007 at 5:34 am |
ah, finally a smart way of doing this! Thank you!
I’m sick and tired of
sprintf(buf, “somestring_%i”,counter++);
string name(buf);
which neither generic, readable nor .. c++
July 3, 2007 at 5:35 am |
btw, awesome blog. love every bit of it
July 3, 2007 at 10:34 am |
Thanks for the comment!
February 25, 2008 at 8:56 pm |
Hey. Great blog!
One question though, why does the string variable have to be named str?
I tried a different variable name and surprisingly it doesn’t work.
February 26, 2008 at 11:04 am |
Thanks for the comment!
We updated the entry and now we use a different name for the string variable, as “str” is also a member of “stringstream.”
July 21, 2008 at 7:29 am |
I wonder why the implementation of std::string doesn’t include automatic conversion from the standard primitives so that one could continue the pretense that C+ is really an OO language (grin)
Seriously though,
one should be able to write
std::string foo = 123
or even
std:: string foo;
foo.ToInt(123);
January 4, 2009 at 6:54 pm |
Thanks for sharing
this is elegant and simple solution
February 19, 2009 at 7:24 am |
What if I have to concatenate a string with an int using CComBSTR?
October 23, 2009 at 11:59 am |
[...] I found a comment on another guys blog that actually makes it work for any type such as double, float, etc.. It has this code using this template. http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/ [...]
September 16, 2010 at 2:35 am |
Thank you
Nice blog!
October 3, 2010 at 11:40 pm |
Thanks i needed that. Not sure if anyone’s stated this but would only really work uniquely if you keep your integers between -127 and 127, otherwise you will not get a unique string, ie char only goes from -127 to 127 as it is one byte, whereas integer i believe is 2 bytes and goes 0 to 65535 or -32767 to 32767.
I believe if you put in integer higher than 127 it just loops round to -127 and continues.
October 14, 2010 at 8:28 pm |
@sdpagent
have u tried?
i believe its not limited to char type size since the stream takes each number in at a time, and each number is a char like it would read 512 as “(char)53 << (char)49 << (char)50", so dont have to worry about the size of the int or whatever.
May 8, 2011 at 1:35 pm |
Thanks for the post!
That solved my problem.
July 3, 2011 at 4:04 am |
This is not working babe.
August 11, 2011 at 11:02 pm |
hi,
Its a very useful one..
Though i have one question.. what if the integer value contained i as 000012( i=000012). the output it gives as 10.. please clarify why it is so!
August 12, 2011 at 10:43 pm |
That is because 0 prefixed integers are interpreted in base 8 (octal). So, your variable i has octal value 12. 12 in base 8 (012) equals 10 in base 10. HTH
August 16, 2011 at 12:08 am |
Hey Rares.. Thank you so much!:)
November 18, 2011 at 9:49 am |
#include
string s = boost::lexical_cast( number );
http://www.cplusplus.com/forum/beginner/7777/
February 28, 2012 at 11:14 am |
Can this be used in a function? I’ve seen a claim that it can, as in
return out.str();
but isn’t that returning a (plain old, dumb) pointer to memory that is going out of scope?
I’m storing a bunch of different data from a structure into something like an XML DOM, all string data. For now I have a bunch of overloads
SaveData(whereToPutIt, int iValue);
SaveData(whereToPutIt, double dValue);
SaveData(whereToPutIt, char * szValue);
so I can call
SaveData(whereToPutIt, MyStructure.dataField).
The overload for int does the conversion as above and calls the string overload. I’d like to concentrate all the conversions like
SaveData(whereToPutIt, ToStr(MyStructure.dataField) );
and not have all the overloads, because I think it would be more elegant, but I want neither of smart pointers (outside the magnitude of the task I’ve been given), pointers to orphans, or memory leaks. And some global buffer with a macro might make my employer discover that I’m a really a C programmer, not an OO C++ programmer.
October 1, 2012 at 11:20 pm |
Thank you!!
December 22, 2012 at 2:24 am |
Looks like we are sacrificing performance for simplicity…
April 16, 2013 at 6:53 am |
[...] FAQ, Tips & Tricks For Unix, Emacs And Other Useful Tools http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/ [...]