How to convert an int to a string in C++? (Also doubles, floats, etc…)

Here is how I do it. This procedure actually works for any type such as: int, double, float, etc…

Convert an Int Only

#include
#include
#include

using namespace std;

int main()
{
int i = 10;
string s = inToString(i);
return 0;
}

string intToString(int inInt)
{
stringstream ss;
string s;
ss << inInt; s = ss.str(); return s; } [/sourcecode] As a Template So It Works with All Types (int, float, double, etc…)

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/

template
string anyTypeToString(const T& t)
{
std::stringstream ss;
ss << t; return ss.str(); } [/sourcecode] This works really well, I have been using it and it is so simple. Key words: int to string intToString double to string doublToString float to string floatToString

35 Comments

  1. [...] double, etc) October 23, 2009, 9:36 pm by Rhyous Tweet Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  2. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  3. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  4. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  5. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  6. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  7. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  8. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  9. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  10. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  11. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  12. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  13. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  14. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  15. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  16. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  17. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  18. [...] How to convert an int to a character array when you don’t have access to the C++ Standard Library? Filed under: Coding — rhyous @ 3:44 am Ok, so I have a great way to convert and int (or any other type) to a string using the stringstream from the standard library. This is listed here: How to convert an int to a string in C++? (Also doubles, floats, etc…) [...]

  19. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  20. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  21. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  22. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  23. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  24. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  25. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  26. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  27. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  28. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  29. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  30. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  31. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  32. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  33. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  34. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

  35. [...] double, etc) Filed under: Coding — rhyous @ 9:36 pm Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in [...]

Leave a Reply to How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

How to post code in comments?