473,385 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

string to integer

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
5 141897

"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
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

"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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
8
by: KRoy | last post by:
I have a password stored in the Registry encrypted using System.Security.Cryptography DES Algorithm. I supplied it a password and a Initialization Vector. I am trying to decrypt it using the...
5
by: jan axelson | last post by:
My application is using RegisterDeviceNotification() to detect attachment and removal of a USB HID-class device. The form is receiving WM_DEVICECHANGE messages with wParam set to...
5
by: Sakharam Phapale | last post by:
Hi All, How to declare the following statement in following structure. szPname As String * MAXPNAMELEN Public Structure MIXERCAPS public wMid As Integer public ...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
2
by: =?Utf-8?B?QXNzaWRv?= | last post by:
Hello @all i need help with the following problem: Im calling an unmanaged C++ DLL (TAPI32.dll) function (lineGetCallInfo) like this: Declare Function lineGetCallInfo Lib "tapi32.dll"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.