Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How do you change an integer to a string in c++?
Solution: Use sprintf() function.
The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you'll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.
To convert the int to char in C language, we will use the following 2 approaches: Using typecasting. Using sprintf()
The itoa() function coverts the integer n into a character string. The string is placed in the buffer passed, which must be large enough to hold the output.
Standard C++ library style:
#include <sstream>
#include <string>
(...)
int number = 5;
std::stringstream ss;
ss << number;
std::string numberAsString(ss.str());
Or if you're lucky enough to be using C++11:
#include <string>
(...)
int number = 5;
std::string numberAsString = std::to_string(number);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With