473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 141932

"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_str ing);
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_str ing);
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
3700
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 discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
0
2752
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 tool for breaking ties when a phonic call returns a bunch of possibilities. Also, I'm looking for someone that has a zip code file with alternate city names (the PO assigns whatever name is convenient to them), email me if you got something. ...
35
2588
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# newsgroup on 25 September. The regular expressions where in that newsgroup too involved. I told yesterday night, to Jay that I would test all 4 methods and the stupid method I was thinking of the first time that night when I saw Jon's
9
7136
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 when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
8
5235
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 CryptoAPI in VB6. I am using the CryptDeriveKey to generate a session key from a password. But it is not working and I am sure the password is correct. In .net I supplied an IV, when and how do I do that using
5
7741
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 DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE. I want to identify the device that has arrived or been removed by examining the dbcc_name member of the DEV_BROADCAST_DEVICEINTERFACE structure.
5
4612
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 wPid As Integer public vDriverVersion As Long
12
3233
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
1
3064
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 a Visual Basic 2005 solution that manipulates strings. It will parse a string containing a list of items within a text box and put the individual items into the list box. It will build the textbox string by putting the list box items together into a...
2
2101
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" (ByVal hCall As Integer, ByRef lpCallInfo As LINECALLINFO) As Integer
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.