Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How do you change an integer to a string? [duplicate]

Tags:

c++

visual-c++

Possible Duplicate:
Alternative to itoa() for converting integer to string C++?

How do you change an integer to a string in c++?

like image 417
Boom_mooB Avatar asked Apr 19 '12 02:04

Boom_mooB


People also ask

How do you convert integer to string in C?

Solution: Use sprintf() function.

How do you convert integers to strings?

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.

Can we convert int to char in C?

To convert the int to char in C language, we will use the following 2 approaches: Using typecasting. Using sprintf()

What does itoa do in C?

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.


1 Answers

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);
like image 82
Asik Avatar answered Oct 17 '22 10:10

Asik