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 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
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
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());
*/
}
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |