By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

string to integer

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Steve" <nospam@nopes> wrote in message
news:40********@clarion.carno.net.au...
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
Jul 22 '05 #2

P: n/a
Steve <nospam@nopes> wrote:
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
Jul 22 '05 #3

P: n/a

"Steve" <nospam@nopes> wrote in message
news:40********@clarion.carno.net.au...
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?

Try this link:
http://www.parashift.com/c++-faq-lit...al-issues.html

Regards,
Sumit.
Jul 22 '05 #4

P: n/a
Martin Gieseking wrote:
Steve <nospam@nopes> wrote:
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


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
Jul 22 '05 #5

P: n/a


John Harrison wrote:
"Steve" <nospam@nopes> wrote in message
news:40********@clarion.carno.net.au...
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

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 ----
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.