string to integer


S

Steve

Hi,

How can I convert a C++ string to an integer and then an integer back to
a string? Is there built-in support for this?

Thanks,

Steve
 
Ad

Advertisements

J

John Harrison

Steve said:
Hi,

How can I convert a C++ string to an integer and then an integer back to
a string? Is there built-in support for this?

Thanks,

Yes lots of support. But what do you mean by string? For C style strings you
can use sprintf (int to string) or sscanf (string to int). For C++ strings
you use stringstream classes.

#include <sstream>
#include <string>
using namespace std;

// string to int
string some_string;
istringstream buffer(some_string);
int some_int;
buffer >> some_int;

// int to string
int some_int;
ostringstream buffer;
buffer << some_int;
string some_string = buffer.str();

You might notice that string streams use exactly the same reading and
writing methods as other streams (cin, cout etc). This is not a coincidence,
and makes string streams useful for a lot of things besides converting
strings to integers.

john
 
M

Martin Gieseking

Steve said:
How can I convert a C++ string to an integer and then an integer back to
a string? Is there built-in support for this?

You can either call the functions atoi and itoa declared in stdlib or use
stringstreams for conversion:

int str2int (const string &str) {
stringstream ss(str);
int n;
ss >> n;
return n;
}

string int2str (int n) {
stringstream ss;
ss << n;
return ss.str();
}

Another possibility is the lexical_cast template from the boost libraries
(www.boost.org).

Martin
 
A

Alan Johnson

Martin said:
You can either call the functions atoi and itoa declared in stdlib or use
stringstreams for conversion:

int str2int (const string &str) {
stringstream ss(str);
int n;
ss >> n;
return n;
}

string int2str (int n) {
stringstream ss;
ss << n;
return ss.str();
}

Another possibility is the lexical_cast template from the boost libraries
(www.boost.org).

Martin

To nitpick, the C++ standard doesn't define any function called itoa.
Use sprintf if you want to work with C-style strings and care about
portability.

Alan
 
Ad

Advertisements

S

Steve

John said:
Yes lots of support. But what do you mean by string? For C style strings you
can use sprintf (int to string) or sscanf (string to int). For C++ strings
you use stringstream classes.

#include <sstream>
#include <string>
using namespace std;

// string to int
string some_string;
istringstream buffer(some_string);
int some_int;
buffer >> some_int;

// int to string
int some_int;
ostringstream buffer;
buffer << some_int;
string some_string = buffer.str();

You might notice that string streams use exactly the same reading and
writing methods as other streams (cin, cout etc). This is not a coincidence,
and makes string streams useful for a lot of things besides converting
strings to integers.

john


Thanks guys, this helped a lot! :)

Cheers,

Steve

------------ And now a word from our sponsor ---------------------
For a secure high performance FTP using SSL/TLS encryption
upgrade to SurgeFTP
---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top