473,405 Members | 2,415 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,405 software developers and data experts.

Converting string to integer

Gil
I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??
Jul 22 '05 #1
13 2454
> I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??

assuming you include stdlib.h and the string-class provides the
cast-operator to const char*:

int val = atoi(string);

Jul 22 '05 #2

"Gil" <br**************@hotmail.com> wrote in message
news:ad*************************@posting.google.co m...
I have a string :

string A = "2";

I need to get it's integer value :


Check http://www.groups.google.com

Anyways,
int main()
{
string str = "123";
istringstream iss(str);
int x;
iss >> x;
if(iss)
{
// conversion successful
}
else
{
// could not convert str to an int
}
}

Best wishes,
Sharad
Jul 22 '05 #3

"Patrik Stellmann" <st*******@tu-harburg.de> wrote in message
news:bv************@uni-berlin.de...
I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ?? assuming you include stdlib.h and the string-class provides the
cast-operator to const char*:


Sorry, but both of these assumptions are incorrect. The standard header to
include is <cstdlib> nowadays. Furthermore the string class does not provide
an implicit const char* conversion but rather the c_str() member function.

int val = atoi(string);


int val = atoi( A.c_str() );

should do the trick.

Regards
Chris
Jul 22 '05 #4

"Gil" <br**************@hotmail.com> skrev i en meddelelse
news:ad*************************@posting.google.co m...
I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??


Do yourself two favours:

1) Get the FAQ for this newsgroup and read it all... it is well worth the
effort. The faq can be found at http://www.parashift.com/c++-faq-lite/.

2) Get the boost-library (http://www.boost.org) and look at all the
wonderful things it has to offer - among them the solution to your problem.

Kind regards
Peter
Jul 22 '05 #5
br**************@hotmail.com (Gil) wrote in message news:<ad*************************@posting.google.c om>...
I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??


hallo

i think you can do so which the function atoi.

int val = atoi(A);

Armando
Jul 22 '05 #6
> > > I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ?? assuming you include stdlib.h and the string-class provides the
cast-operator to const char*:


Sorry, but both of these assumptions are incorrect. The standard header to
include is <cstdlib> nowadays. Furthermore the string class does not provide
an implicit const char* conversion but rather the c_str() member function.


He didn't mention any details about his string class. Maybe you are
confusing it with std::string ?
int val = atoi( A.c_str() );


int val = std::atoi( A.c_str() );

nowadays.
Jul 22 '05 #7
On Wed, 28 Jan 2004 09:32:08 +0100, "Chris Theis"
<Ch*************@nospam.cern.ch> wrote in comp.lang.c++:

"Patrik Stellmann" <st*******@tu-harburg.de> wrote in message
news:bv************@uni-berlin.de...
I have a string :

string A = "2";

I need to get it's integer value :

// ??
int val = A ; // ??

assuming you include stdlib.h and the string-class provides the
cast-operator to const char*:


Sorry, but both of these assumptions are incorrect. The standard header to
include is <cstdlib> nowadays. Furthermore the string class does not provide
an implicit const char* conversion but rather the c_str() member function.

int val = atoi(string);


int val = atoi( A.c_str() );

should do the trick.


No, not really. The ato... functions from the C library should never
be used unless you have already checked the character array and know
that it will not result in a numeric value outside the range of the
given type, in which case you might as well do the conversion yourself
while you are walking the string.

All of the ato... functions generate undefined behavior if the text
string represents a numerical value outside the range of the type.

That is exactly why the strto... functions were added during C
standardization. They have defined behavior for all inputs, other
than being passed a null pointer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #8

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om...
> I have a string :
>
> string A = "2";
>
[SNIP]
He didn't mention any details about his string class. Maybe you are
confusing it with std::string ?
The OP declared his string using a class named "string". Without any further
specification it is valid (at least IMHO) to presume that the standard
string class is meant. Anyway, naming your own string class like the
standard one is certainly not the cleverest idea, don't you think?

int val = std::atoi( A.c_str() );

nowadays.


Only if no using namespace statement has been issued, which cannot be
verified from the code snippet.

Chris
Jul 22 '05 #9

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:70********************************@4ax.com...
On Wed, 28 Jan 2004 09:32:08 +0100, "Chris Theis"
<Ch*************@nospam.cern.ch> wrote in comp.lang.c++:
[SNIP]
No, not really. The ato... functions from the C library should never
be used unless you have already checked the character array and know
that it will not result in a numeric value outside the range of the
given type, in which case you might as well do the conversion yourself
while you are walking the string.

All of the ato... functions generate undefined behavior if the text
string represents a numerical value outside the range of the type.

That is exactly why the strto... functions were added during C
standardization. They have defined behavior for all inputs, other
than being passed a null pointer.


I didn't know that ato... might result in undefined behavior. Thanks for
telling me, Jack. However, in C++ I would recommend to use stringstreams
anyway.

Cheers
Chris
Jul 22 '05 #10
Chris Theis wrote:
The OP declared his string using a class named "string". Without any further
specification it is valid (at least IMHO) to presume that the standard
string class is meant. Anyway, naming your own string class like the
standard one is certainly not the cleverest idea, don't you think?

I don't even think it's a legal identifier. I believe that in C++, like
C, all identifiers beginning with "str" and followed by a letter are
reserved.


Brian Rodenborn
Jul 22 '05 #11
Default User wrote:
Chris Theis wrote:
[redacted] I don't even think it's a legal identifier. I believe that in C++, like
C, all identifiers beginning with "str" and followed by a letter are
reserved.


Say what??????????????? Can you quote chapter and verse from the Holy
Standard (or the Holy C standard) on that one?

That would break any code that uses "strange", "strong", "stretch",
etc.. as variables.

Jul 22 '05 #12

"red floyd" <no*****@here.dude> wrote in message news:%7*******************@newssvr25.news.prodigy. com...
Default User wrote:
Chris Theis wrote:

[redacted]
I don't even think it's a legal identifier. I believe that in C++, like
C, all identifiers beginning with "str" and followed by a letter are
reserved.


Say what??????????????? Can you quote chapter and verse from the Holy
Standard (or the Holy C standard) on that one?

He's not quite got it right. The C standard reserves external symbols that
start with str followed by a lower case letter for future use.

You can use them internal to a function or class (or other internal linkage) or
in a namespace other than std or the global namespace.

Jul 22 '05 #13
Ron Natalie wrote:
He's not quite got it right. The C standard reserves external symbols that
start with str followed by a lower case letter for future use.
Yes, I forgot to mention the lowercase part.
You can use them internal to a function or class (or other internal linkage) or
in a namespace other than std or the global namespace.


As far as one could tell from example, it was in the global namespace.

Brian Rodenborn
Jul 22 '05 #14

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: 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
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...
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.