By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Char * to int

P: n/a
Kay
Is it possible to convert char * to integer ?
if yes, how to do it ?

Jul 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Kay wrote:
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.)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

P: n/a
"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...
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
Jul 22 '05 #3

P: n/a
"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...
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?").

--
David Hilsee
Jul 22 '05 #4

P: n/a

"Phlip" <ph*******@yahoo.com> wrote in message
news:Nj******************@newssvr17.news.prodigy.c om...
Kay wrote:
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.)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


Isn't using 'atoi' function in <stdlib.h> much simpler?
Jul 22 '05 #5

P: n/a
Method Man wrote:
> 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.)


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.

Jul 22 '05 #6

P: n/a

"Method Man" <a@b.c> wrote in message
news:1L*********************@read2.cgocable.net...

"Phlip" <ph*******@yahoo.com> wrote in message
news:Nj******************@newssvr17.news.prodigy.c om...
Kay wrote:
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.)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


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
Jul 22 '05 #7

P: n/a
Method Man wrote:

"Phlip" <ph*******@yahoo.com> wrote in message
news:Nj******************@newssvr17.news.prodigy.c om...
Kay wrote:
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.)

--
Phlip

http://industrialxp.org/community/bi...rstUserInterfa
ces


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
Jul 22 '05 #8

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:f6*****************@newsread1.news.pas.earthl ink.net...

"Method Man" <a@b.c> wrote in message
news:1L*********************@read2.cgocable.net...

"Phlip" <ph*******@yahoo.com> wrote in message
news:Nj******************@newssvr17.news.prodigy.c om...
Kay wrote:

> 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.)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


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


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

P: n/a
"Method Man" <a@b.c> wrote in message
news:76*****************@read1.cgocable.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:f6*****************@newsread1.news.pas.earthl ink.net...

"Method Man" <a@b.c> wrote in message
news:1L*********************@read2.cgocable.net...

"Phlip" <ph*******@yahoo.com> wrote in message
news:Nj******************@newssvr17.news.prodigy.c om... >
> (strtol() also works.) 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


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
Jul 22 '05 #10

P: n/a
Kay <er*********@yahoo.com.hk> wrote:
Is it possible to convert char * to integer ?
if yes, how to do it ?


<http://www.boost.org/libs/conversion/lexical_cast.htm>

int main(int argc, char * argv[])
{
using boost::lexical_cast;
using boost::bad_lexical_cast;

std::vector<short> args;

while(*++argv)
{
try
{
args.push_back(lexical_cast<short>(*argv));
}
catch(bad_lexical_cast &)
{
args.push_back(0);
}
}
...
}

There you go...
Jul 22 '05 #11

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:qg****************@newsread1.news.pas.earthli nk.net...
"Method Man" <a@b.c> wrote in message
news:76*****************@read1.cgocable.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:f6*****************@newsread1.news.pas.earthl ink.net...

"Method Man" <a@b.c> wrote in message
news:1L*********************@read2.cgocable.net...
>
> "Phlip" <ph*******@yahoo.com> wrote in message
> news:Nj******************@newssvr17.news.prodigy.c om... >
> > (strtol() also works.) 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


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


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.
Jul 22 '05 #12

P: n/a
Method Man wrote:

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 :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #13

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Method Man wrote:

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 (sscanf(String, "%d", &Result) == 1) ? Result : 0;
return Result;
}


-Mike
Jul 22 '05 #14

P: n/a
Method Man wrote:
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!)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #15

P: n/a
Phlip wrote:
Method Man wrote:
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,


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.

Jul 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.