473,385 Members | 1,707 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,385 software developers and data experts.

atoi function

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
5 36397
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mark | last post by:
can someone point me to the implementation or algorithm of atoi function
8
by: Sonia | last post by:
Hi, I've been using atoi for a while now, but would like to know how to implement one? Can anyone give me simple efficient implementation of that function? Or point me sowhere for reference. ...
15
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
11
by: martin paul | last post by:
Sir please consider the following code...... int getint(char *); int main () { char name; printf("enter the string\n"); scanf("%s",name); getint(name); } ...
2
by: durgapavani | last post by:
char str1 = "124z3yu87"; char str2 = "1000"; char *str3 = "100"; printf("str1: %d\n", atoi(str1)); printf("str2: %d\n", atoi(str2)); printf("str3: %d\n", atoi(str3)); str1: 124...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
2
by: Andrea Robinson | last post by:
I need to convert data that is input from a file from ASCII chars to Ints, but the catch is no atoi and I can ONLY convert number characters and ignore the others, and the # symbol has to act like a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.