Char * to int

P

Phlip

Kay said:
Is it possible to convert char * to integer ?
if yes, how to do it ?

char const * z = "12";

stringstream stream = z;
int q = 0;

if (z >> q)
cout << "yes" << endl;

(strtol() also works.)
 
M

Mike Wahler

Kay said:
Is it possible to convert char * to integer ?

No, not portably.
if yes, how to do it ?

Perhaps you want to convert the textual representation
of a number to an integer type. If so, use a stringstream.

#include <iostream>
#include <sstream>

int main()
{
const char *x = "123";
std::istringstream iss(x);
int i(0);

if(iss >> i)
std::cout << i << '\n';
else
std::cerr << "unable to convert\n";

return 0;
}


-Mike
 
D

David Hilsee

Kay said:
Is it possible to convert char * to integer ?
if yes, how to do it ?

The technique listed in the FAQ for converting std::strings to numbers will
also work for C-style strings. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 38 ("Miscellaneous
technical issues"), question 2 ("How do I convert a std::string to a
number?").
 
M

Method Man

Phlip said:
char const * z = "12";

stringstream stream = z;
int q = 0;

if (z >> q)
cout << "yes" << endl;

(strtol() also works.)

Isn't using 'atoi' function in <stdlib.h> much simpler?
 
R

Rolf Magnus

Method said:
Isn't using 'atoi' function in <stdlib.h> much simpler?

Yes, but it's also much worse, because it has a design bug. It doesn't
provide any way to find out whether the conversion was successful.
 
M

Mike Wahler

Method Man said:
Isn't using 'atoi' function in <stdlib.h> much simpler?

Yes, but it cannot be used safely. (Does not protect from
overflow, which would cause undefined behavior. Also, a
valid result of zero cannot be distinguished from an error,
which 'atoi()' indicates by returning zero.

-Mike
 
D

Default User

Method said:
Isn't using 'atoi' function in <stdlib.h> much simpler?

Simpler, perhaps. It has no error handling capability though. It's
generally recommended to you strtol() if you want to use C-style
functionality.


Brian Rodenborn
 
M

Method Man

Mike Wahler said:
Yes, but it cannot be used safely. (Does not protect from
overflow, which would cause undefined behavior. Also, a
valid result of zero cannot be distinguished from an error,
which 'atoi()' indicates by returning zero.

-Mike

Hmm, I would just write my own version of 'atoi' then. At the least, it's
good practice for a C/C++ job interview. ;)
 
M

Mike Wahler

Method Man said:
Hmm, I would just write my own version of 'atoi' then. At the least, it's
good practice for a C/C++ job interview. ;)

Why would you not use 'strto(u)l()'?

If a candidate wasted time reinventing what the standard library
already has, I would not be impressed. I'd be more impressed with
one who knows which tools are available, and when/how/where to apply
them.

Anyone familiar with C or C++ will recognize 'strtol()' at a glance,
whereas upon encountering a 'home-grown' conversion function, one
would need to find out what the darn thing actually does. Also, a
standard library function has a far better chance of being fully
debugged and robust.


-Mike
 
M

Method Man

Mike Wahler said:
Why would you not use 'strto(u)l()'?

If a candidate wasted time reinventing what the standard library
already has, I would not be impressed. I'd be more impressed with
one who knows which tools are available, and when/how/where to apply
them.

Anyone familiar with C or C++ will recognize 'strtol()' at a glance,
whereas upon encountering a 'home-grown' conversion function, one
would need to find out what the darn thing actually does. Also, a
standard library function has a far better chance of being fully
debugged and robust.


-Mike

Yes, you're right.

I guess I've always used 'atoi'. This is what I was taught back in my 1st
year university CS class for converting any C-style strings to int.

Regarding the interview question. I've actually been asked to specifically
implement 'atoi' in two separate interviews. I suppose the context was
simply that it's a good test for one's pointer knowledge and error handling
ability with strings.
 
K

Karl Heinz Buchegger

Method said:
I guess I've always used 'atoi'. This is what I was taught back in my 1st
year university CS class for converting any C-style strings to int.

Regarding the interview question. I've actually been asked to specifically
implement 'atoi' in two separate interviews. I suppose the context was
simply that it's a good test for one's pointer knowledge and error handling
ability with strings.

int atoi( char* String )
{
int Result;
sscanf( "%d", Result );
return Result;
}

:)

I guess this is not what your interviewer expected you to do :)
 
P

Phlip

Method said:
Hmm, I would just write my own version of 'atoi' then. At the least,
it's good practice for a C/C++ job interview. ;)

If asked to write atoi() at a job interview, I would match its exact ISO
specification, verbally point out all its minor flaws, reveal when and when
not I would use it, and write wrapper code that defends its calls.

(Not sure if I could match the performance with lexical_cast, though!)
 
R

Rolf Magnus

Phlip said:
If asked to write atoi() at a job interview, I would match its exact ISO
specification, verbally point out all its minor flaws,

It has minor flaws too?
reveal when and when not I would use it, and write wrapper code that
defends its calls.

If asked to write it, I'd write it. Nothing more. If they want to know more,
they can ask. Well, maybe I'd just note that atoi() is a bad idea because
has a major design bug.
It's just like in oral exams. Don't answer questions that have not been
asked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top