atoi function

B

Bansidhar

atoi() function seems not to have any support for Hex, octal number.
Usually when I read from a text file then it contain number like
0x232 etc. In this case atoi() fells. In case of itoa() there
is arrangement of passing the radix. Is there any historical
reason that it is not the case with atoi() ? Is there any alternate
function in C,C++ for atoi() to take care of all these things ?

--Bansidhar
 
N

nrk

Bansidhar said:
atoi() function seems not to have any support for Hex, octal number.
Usually when I read from a text file then it contain number like
0x232 etc. In this case atoi() fells. In case of itoa() there
is arrangement of passing the radix. Is there any historical
reason that it is not the case with atoi() ? Is there any alternate
function in C,C++ for atoi() to take care of all these things ?

--Bansidhar

There's no itoa in standard C. Don't know about the history of atoi, but
standard C provides strtol that allows you to convert strings in base other
than decimal. Another option is sscanf with the "%i" format specifier.
Read your favorite C book or compiler documentation for more information.

-nrk.
 
R

Richard Bos

atoi() function seems not to have any support for Hex, octal number.
Usually when I read from a text file then it contain number like
0x232 etc. In this case atoi() fells. In case of itoa() there
is arrangement of passing the radix. Is there any historical
reason that it is not the case with atoi() ? Is there any alternate
function in C,C++ for atoi() to take care of all these things ?

There is no itoa() in ISO C, so any implementation which provides it as
an extension is free to define it as it wishes - it may be true that
your itoa() takes a radix, but this is by no means guaranteed.
As for atoi(), it is usually not a good function to use anyway. As you
note, it doesn't understand anything but decimal, but what's worse, it
has no facility to tell you where it stopped translating, and it will
cause undefined behaviour if you pass it a number too large for it. To
cure all these problems, use strtol() instead.

Richard
 
E

Emmanuel Delahaye

In said:
atoi() function seems not to have any support for Hex, octal number.
Usually when I read from a text file then it contain number like
0x232 etc. In this case atoi() fells. In case of itoa() there
is arrangement of passing the radix. Is there any historical
reason that it is not the case with atoi() ? Is there any alternate
function in C,C++ for atoi() to take care of all these things ?

The is no itoa() is the standard C library. atoi() is obsolete and acts like
strtol() with base 0. (Auto detection of decimal/octal/hexadecimal patterns)

Decimal : [+-]0|1-9[0-9[*]]
Octal : [+-]0[0-7[*]]
Hexadecimal : [+-]0x0-9a-fA-F[0-9a-fA-F[*]]

strtol() and strtoul() are recommended instead of atoi(). You can also choose
a base from 0 to 36. Open your C-book for detail.
 
J

Jeremy Yallop

Emmanuel said:
The is no itoa() is the standard C library. atoi() is obsolete and acts like
strtol() with base 0. (Auto detection of decimal/octal/hexadecimal patterns)

No, it acts like strtol() with base 10, except for the behaviour on
error, which is particularly bad: for example, if the value of the
integer is outside the range of an int the behaviour is undefined.

Jeremy.
 
P

Peter Pichler

Emmanuel Delahaye said:
You can also choose
a base from 0 to 36. Open your C-book for detail.

Actually, the base could be between 2 and 36 and 0. The sdandard folk
probably considered the base 1 somehow inferior ;-)
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top