473,396 Members | 1,784 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,396 software developers and data experts.

Converting a String to an Integer

Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";
I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?
Thank You
Sakitah
Reply

Oct 5 '05 #1
7 2352
sakitah wrote:
Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";
I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?


Search Google Groups for 'conversion int to string c++'. And please
do search Google for answers before posting. Otherwise you're wasting
your precious time waiting for an answer here.

V
Oct 5 '05 #2
sakitah wrote:
Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";
I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?
Thank You
Sakitah
Reply


Please consult this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-39.2

Cheers! --M

Oct 5 '05 #3
I hope this helps you.
As a word of advise, stay with the C++ style, but I am giving you the
two methodologies.

This is just a snip of a class I wrote to handle this kind of problem,
if you want the full class, just write to me directly using the same
subject title and I will send you a copy, OK?
--snip
const int jme::strtools::toInt( const std::string& s ) throw (
jme::strtoolsEx ) {
//Test the string
if ( s.empty() )
{
throw jme::strtoolsEx( str_empty, FILE, METHOD, LINE );
}
// This is what you want <<==
std::istringstream iss( s );
int num;
//If the data extracted from 'iss' is successfully assingned to
// num and iss retuns false
if ( iss >> num && iss.eof() )
return num;
else
throw jme::strtoolsEx( fe_eof_bit, FILE, METHOD, LINE ); <<==
this is my own exception, but it gives you an idea as to how to hadle
the problem.

/*
// The simple 'C' version
if(s.empty()) throw jme::strtoolsEx(STR_EMPTY,FILE,METHOD,LINE);
return atoi(s.c_str());
*/
}

Oct 5 '05 #4
sakitah wrote:
Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";
I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?
Thank You
Sakitah
Reply


On top of what's been mentioned you may also want to look up strtol
Oct 6 '05 #5


sakitah wrote:
Hello Everyone,

Here's the problem: I have a string that I break down into 3 other
strings as such:

string1 = "001-202-1234567"

and parsing it into:

string2 = "001";
string3 = "202";
string4 = "1234567";
I want to manipulate these as numbers. I used the append function for
strings to fill the new strings from the original string. Sadly, I
cant append from the original string into an integer, so how can I do
it?
Thank You
Sakitah
Reply

From - Wed


Depending on what type of string you are using, you will need to use 2
types of functions.

if the string is
std:string
then use atoi()
if it is LPCTSTR then use
strtol

both are in the MSDN!

Daniel moree
Oct 6 '05 #6
Daniel Moree wrote:

if the string is
std:string
then use atoi()
if it is LPCTSTR then use
strtol


Why atoi() with std::string ?

Never recommend atoi(). It has a serious design flaw:
You cannot figure out if the conversion worked or produced
an error. atoi() is in the same category as gets(). Never, ever
recommend those functions. It would have been better if those
functions never were invented.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 6 '05 #7
Karl Heinz Buchegger wrote:
Daniel Moree wrote:
if the string is
std:string
then use atoi()
if it is LPCTSTR then use
strtol


Why atoi() with std::string ?

Never recommend atoi(). It has a serious design flaw:
You cannot figure out if the conversion worked or produced
an error. atoi() is in the same category as gets(). Never, ever
recommend those functions. It would have been better if those
functions never were invented.

Further, in the case of an overflow, the behavior is undefined.
That's even worse than just not being able to differentiate between
0 and an error return.
Oct 6 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
7
by: RCS | last post by:
Okay, a rather 'interesting' situation has arisen at a place I work: I need to convert a database from Access to something that can be used over the web. I am currently maintaining and...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
0
by: Mark Allen | last post by:
Hello, I am creating an RTF document server side for a report. However I am having problems converting images into the required RTF format. I am converting the image into a string (binary)...
11
by: Steve | last post by:
I'm hoping someone can help me out. I'm a newbie to vb.net still. I'm trying to convert the code below from VB6 to VB.NET. I'm not sure of the best way to go. This is basically a simple...
4
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.