« Correcting IP address of PHP behind a reverse proxy | Home | TilEm, a TI-83 Plus emulator for Linux »
Converting between int and std::string in C++
By admin | November 20, 2010
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int main() {
// convert int to string
int some_int = 52;
stringstream ss;
ss << some_int;
string result_s;
ss >> result_s;
// result_s = 52
cout << result_s << endl;
// convert string to int
string some_string = "1234";
int result_i = atoi(some_string.c_str());
// result_i = 1234
cout << result_i << endl;
}
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!
Topics: Linux, Windows | 12 Comments »

September 14th, 2011 at 08:42
What about converting from std::string back to int
April 28th, 2012 at 19:13
@Russell:
See atoi() or sscanf() in the C standard library.
August 3rd, 2025 at 05:40
… [Trackback]
[…] Find More Info here to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
August 3rd, 2025 at 06:48
… [Trackback]
[…] Find More on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
August 10th, 2025 at 00:59
… [Trackback]
[…] Read More to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
August 30th, 2025 at 19:07
… [Trackback]
[…] Here you can find 49665 more Information on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
September 3rd, 2025 at 08:08
… [Trackback]
[…] Information to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
September 3rd, 2025 at 22:05
… [Trackback]
[…] Find More here to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
September 19th, 2025 at 18:24
… [Trackback]
[…] Find More here to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
October 4th, 2025 at 11:05
… [Trackback]
[…] Find More here on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
October 6th, 2025 at 23:24
… [Trackback]
[…] Info on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]
October 9th, 2025 at 22:04
… [Trackback]
[…] Find More Info here on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]