Stringstreams:
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char *argv[]) {
// long to string
int n = 123456789;
ostringstream oss;
oss << n;
string s(oss.str());
cout << s << endl;
// string to long
n = 0;
istringstream iss(s);
iss >> n;
cout << n << endl;
return 0;
}
If you only have to do it once and don't want to instantiate the stringstream you could use a standard C function like strtol():
Code:
#include <cstdlib>
string s("123");
int n = strtol(s.c_str(), NULL, 0);
But there is no simple inverse of that.