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

atoi function

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
nrk
Bansidhar wrote:
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.

--
Remove devnull for email
Nov 14 '05 #2

P: n/a
db********@indiainfo.com (Bansidhar) wrote:
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
Nov 14 '05 #3

P: n/a
In 'comp.lang.c', db********@indiainfo.com (Bansidhar) wrote:
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.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #4

P: n/a
Emmanuel Delahaye wrote:
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.
Nov 14 '05 #5

P: n/a
"Emmanuel Delahaye" <em**********@noos.fr> wrote in message
news:Xn***************************@213.228.0.4...
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 ;-)
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.