Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
ConvertingAnIntegerToaStringInCpp
Edit
PageHistory
Diff
Info
LikePages
We seem to be getting a hit a day trying to figure out how to converting an Integer to a String in C++, so I thought I'd better write a page about it. !!Method One: snprintf(3) This technically gives you a char*, not a "String", but who's counting eh? <verbatim> int i2str(int i) { int i; char s[[256]; snprintf(s,sizeof(s),"%i",i); return strdup(s); } </verbatim> !!Method Two: use a stringstream <verbatim> #include <sstream> #include <string> std::string i2string(int i) { std::ostringstream buffer; buffer << i; return buffer.str(); } </verbatim> or: !!Method Three: Anything to string <verbatim> template<class T> std::string any2string(T i) { std::ostringstream buffer; buffer << i; return buffer.str(); } </verbatim> CategoryProgramming
One page links to
ConvertingAnIntegerToaStringInCpp
:
InNeedOfRefactor