Convert Integer to String in C++
Convert an int to a std::string in C++ with to_string or a stringstream. See when each is handy, how to pad with zeros, and why char(num) is not the same thing.
1#include <iostream>2#include <string>3#include <sstream>4using namespace std;56int main() {7 int num = 12345;89 // Method 1: Using to_string()10 string str1 = to_string(num);1112 // Method 2: Using stringstream13 stringstream ss;14 ss << num;15 string str2 = ss.str();1617 cout << "Integer: " << num << endl;18 cout << "String (method 1): " << str1 << endl;19 cout << "String (method 2): " << str2 << endl;2021 return 0;22}
Sample Output
String (method 1): 12345
From to_string.
In this one
- How to_string turns a number into text
- How stringstream << builds text from numbers
- How ss.str() reads the built string
- How to add zero padding with setw and setfill
How it works
- 1
num holds 12345.
- 2
to_string(num) gives "12345".
- 3
ss << num writes the digits into a stringstream.
- 4
ss.str() copies them out as a string.
- 5
Print all three.
Edge cases
Negative number
Set num = -42.
to_string gives "-42", with the minus sign included in the text.
Casting instead of converting
Write string s(1, char(num));
That makes one character with code num, not the digits. For 65 you get "A", not "65".
What's going on
The program converts an integer (like 12345) into a string (like "12345"). This conversion is necessary because:
Example:
Header Files Used
Declaring Variables
The program declares:
int num = 12345;
Method 1: Using to_string() (Integer to String)
string str1 = to_string(num);
This is the most modern and recommended method in C++:
How it works:
Advantages:
Example:
int n = 12345;
string s = to_string(n); // s = "12345"
Method 2: Using stringstream
stringstream ss;
ss << num;
string str2 = ss.str();
This method uses stringstream to convert the integer:
How it works:
Advantages:
Example with formatting:
stringstream ss;
ss << fixed << setprecision(2) << 123.45;
string s = ss.str(); // s = "123.45"
Example with multiple values:
stringstream ss;
ss << "Number: " << 12345 << " is an integer";
string s = ss.str(); // s = "Number: 12345 is an integer"
Other Methods (Mentioned but not shown in code)
Method 3: Using sprintf() (C-style)
#include <cstdio>
char buffer[20];
sprintf(buffer, "%d", num);
string str3 = buffer;
Method 4: Using itoa() (Non-standard, C-style)
#include <cstdlib>
char buffer[20];
itoa(num, buffer, 10); // 10 = base 10 (decimal)
string str4 = buffer;
Method 5: Manual Conversion
string str5 = "";
int temp = num;
if (temp == 0) str5 = "0";
else {
bool negative = temp < 0;
if (negative) temp = -temp;
while (temp > 0) {
str5 = char('0' + temp % 10) + str5;
temp /= 10;
}
if (negative) str5 = "-" + str5;
}
How Manual Conversion Works (Step-by-Step)
Understanding manual conversion helps you understand the concept:
For integer 12345:
The process:
Displaying Results
The program prints:
cout << "Integer: " << num << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
Output:
Integer: 12345
String (method 1): 12345
String (method 2): 12345
Both methods produce the same string value.
When to Use Each Method
Formatting Considerations
With to_string(): Basic conversion, no formatting options
string s = to_string(123.456); // "123.456000" (default precision)
With stringstream: Full formatting control
stringstream ss;
ss << fixed << setprecision(2) << 123.456;
string s = ss.str(); // "123.46"
For integers: to_string() is usually sufficient
For floats/doubles: stringstream gives better control over decimal places
Performance Considerations
For most applications, the performance difference is negligible. Choose based on code clarity and requirements.
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is to_string(5).length()?
Pick an answer to check it.
Question 2
What does "Age: " + to_string(20) give?
Pick an answer to check it.
Questions people ask
Which is faster?
to_string is shorter and usually faster. stringstream is useful when you need formatting like padding or mixing several values.
How do I get "007" from 7?
ss << setw(3) << setfill('0') << 7; with <iomanip>.
Can I add a number to a string with +?
Not directly. Use str + to_string(num).