473,434 Members | 1,538 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,434 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 141903

"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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.