Connecting Tech Pros Worldwide Forums | Help | Site Map

atoi function

Sonia
Guest
 
Posts: n/a
#1: Jul 23 '05
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. Everywhere I look the usage is
described, but I cannot actually view the code itself.
Thanks



Mike Wahler
Guest
 
Posts: n/a
#2: Jul 23 '05

re: atoi function



"Sonia" <smach02@hotmail.com> wrote in message
news:U6dPd.5176$a06.1477@bignews1.bellsouth.net...[color=blue]
> Hi,
> I've been using atoi for a while now,[/color]

Note that 'atoi()' cannot prevent the possibility
of overflow (and the resulting undefined behavior).
Prefer 'strtol()' or an ostringstream with operator<<.
[color=blue]
> but would like to know how to
> implement one?[/color]

Parse the string for digit characters, convert them
to numeric values, and multiply each by the appropriate
power of ten according to its digit position. Sum the
results of these multiplications. Return the sum.
If the string is not convertible (i.e. invalid characters),
return zero.
[color=blue]
> Can anyone give me simple efficient implementation of that function?[/color]

The simplest most efficient implementation is almost
certainly the one supplied with your compiler.
[color=blue]
> Or point me sowhere for reference.[/color]

See a book, newsgroup or website on algorithms.
[color=blue]
> Everywhere I look the usage is
> described, but I cannot actually view the code itself.[/color]

The code itself will vary among C++ implementations (and
is very likely written in assembly code.)

-Mike


Brooke
Guest
 
Posts: n/a
#3: Jul 23 '05

re: atoi function


Here is a start...


#include <stdlib>
#define ASC_ZERO 48

int Int2Str(int iNum, char* str)
{
char* Refstr = "0123456789";
int iRem;
int iCnt = 0;
int iflag = 0;

if(iNum < 0)
{
iflag = 1;
iNum *= -1;
}

str[63 - iCnt] = 0;
do
{
iCnt ++;
iRem = iNum % 10;
iNum = iNum / 10;
str[63 - iCnt] = Refstr[iRem];
}while(iNum);

iCnt = 63 - iCnt - iflag;

if(iflag) str[iCnt] = '-';

return(iCnt);
}

void main()
{
int iNumber = 23456778;
char str[64];
int iIndex = 0;
iIndex = Int2Str(2.2, str);
printf("%s\r\n", &(str[iIndex]));
getchar();
return;
}

"Sonia" <smach02@hotmail.com> wrote in message
news:U6dPd.5176$a06.1477@bignews1.bellsouth.net...[color=blue]
> 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. Everywhere I look the usage is
> described, but I cannot actually view the code itself.
> Thanks
>[/color]


Ivan Vecerina
Guest
 
Posts: n/a
#4: Jul 23 '05

re: atoi function


"Sonia" <smach02@hotmail.com> wrote in message
news:U6dPd.5176$a06.1477@bignews1.bellsouth.net...[color=blue]
> 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. Everywhere I look the usage is
> described, but I cannot actually view the code itself.[/color]
The function, without any error handling, could be written
like this:
int atoi(char const* str)
{
int result = 0;
while( char c = *str++ ) // for each non-NUL digit
result = result*10 + (c-'0'); // multiply previous result by 10
// and add as units the value of the digit,
// obtained by subtracting the character value of '0'
return result;
}



--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Mike Wahler
Guest
 
Posts: n/a
#5: Jul 23 '05

re: atoi function



"Ivan Vecerina" <INVALID_use_webform_instead@vecerina.com> wrote in message
news:cujusg$887$1@news.hispeed.ch...[color=blue]
> "Sonia" <smach02@hotmail.com> wrote in message
> news:U6dPd.5176$a06.1477@bignews1.bellsouth.net...[color=green]
> > 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. Everywhere I look the usage is
> > described, but I cannot actually view the code itself.[/color]
> The function, without any error handling, could be written
> like this:
> int atoi(char const* str)
> {
> int result = 0;
> while( char c = *str++ ) // for each non-NUL digit
> result = result*10 + (c-'0'); // multiply previous result by 10
> // and add as units the value of the digit,
> // obtained by subtracting the character value of '0'
> return result;
> }[/color]

Can you guarantee that if I call your atoi() with:

int i = atoi("x");

... that the value of 'i' will be zero, as does the standard 'atoi()'?

:-)

-Mike


Jack Klein
Guest
 
Posts: n/a
#6: Jul 23 '05

re: atoi function


On Sat, 12 Feb 2005 03:24:34 GMT, "Brooke" <brooke@hotmail.com> wrote
in comp.lang.c++:
[color=blue]
> Here is a start...[/color]

Here is a better start. Don't top post. And read the original post
before replying. The OP asked for an implementation that is
equivalent to atoi(), a function that returns the numerical equivalent
of a source text string. What you wrote is just the opposite.
[color=blue]
> #include <stdlib>
> #define ASC_ZERO 48[/color]

No, no, NO!!! The macro above is not only unnecessary, it is EVIL.
It will make this function break horribly on implementations that use
an execution character set that is not ASCII. And you don't use the
evil macro anyway.

The character representing 0 in C++ is '0', always, no matter what the
character set.
[color=blue]
> int Int2Str(int iNum, char* str)
> {
> char* Refstr = "0123456789";[/color]

Completely unnecessary array. And if it was actually necessary,
better defined as:

static const char Refstr [] = "0123456789";

....but it is not needed at all.
[color=blue]
> int iRem;
> int iCnt = 0;
> int iflag = 0;
>
> if(iNum < 0)
> {
> iflag = 1;
> iNum *= -1;[/color]

This can cause undefined behavior, because INT_MIN may be equal to
(-INT_MAX - 1), so -INT_MIN may overflow.
[color=blue]
> }
>
> str[63 - iCnt] = 0;[/color]

What happens if the caller passed a character array of 16 elements?
[color=blue]
> do
> {
> iCnt ++;
> iRem = iNum % 10;
> iNum = iNum / 10;
> str[63 - iCnt] = Refstr[iRem];[/color]

Now you can replace the statement above with:

str[63 = iCnt] = iRem + '0';

....since C++ guarantees that the characters representing the decimal
digits are in contiguous, ascending order.
[color=blue]
> }while(iNum);
>
> iCnt = 63 - iCnt - iflag;
>
> if(iflag) str[iCnt] = '-';
>
> return(iCnt);[/color]

'return' is a statement, not a function call. While parentheses
around the expression are harmless, they have not been required in C
since 1989 and never required in C++.
[color=blue]
> }[/color]
[color=blue]
> void main()[/color]

Oops, you are not programming in C or C++, you just think you are.

In C and C++, main() must be defined with a return type of int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Owen Jacobson
Guest
 
Posts: n/a
#7: Jul 23 '05

re: atoi function


On Fri, 11 Feb 2005 19:44:18 -0500, Sonia wrote:
[color=blue]
> 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. Everywhere I look the usage is
> described, but I cannot actually view the code itself.[/color]

Just for kicks:

#include <string>

int myatoi (const std::string &str) {
using std::string;

int value = 0;
for (string::const_iterator i = str.begin ();
i != str.end ();
++i) {
int digit = *i - '0';
if (digit < 0 || digit > 9)
return 0;
value *= 10;
value += digit;
}

return value;
}

Caveat: totally untested, and I didn't actually read the atoi
documentation before I wrote this. You're probably better off using the
real atoi function anyways, but knowing one way for it to work isn't a bad
thing.

'piny

Ivan Vecerina
Guest
 
Posts: n/a
#8: Jul 23 '05

re: atoi function


"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:DlfPd.6483$UX3.4033@newsread3.news.pas.earthl ink.net...[color=blue]
>
> "Ivan Vecerina" <INVALID_use_webform_instead@vecerina.com> wrote in
> message
> news:cujusg$887$1@news.hispeed.ch...[color=green]
>> "Sonia" <smach02@hotmail.com> wrote in message
>> news:U6dPd.5176$a06.1477@bignews1.bellsouth.net...[color=darkred]
>> > 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. Everywhere I look the usage is
>> > described, but I cannot actually view the code itself.[/color]
>> The function, without any error handling, could be written
>> like this:
>> int atoi(char const* str)
>> {
>> int result = 0;
>> while( char c = *str++ ) // for each non-NUL digit
>> result = result*10 + (c-'0'); // multiply previous result by 10
>> // and add as units the value of the digit,
>> // obtained by subtracting the character value of '0'
>> return result;
>> }[/color]
>
> Can you guarantee that if I call your atoi() with:
>
> int i = atoi("x");
>
> .. that the value of 'i' will be zero, as does the standard 'atoi()'?
> :-)[/color]

I think this could be considered as being part of 'error handling',
which was explicitly omitted.

This said, the code above is flawed in more serious ways:
- a leading + or - shall be processed if present
- leading whitespace should be skipped
But let's leave those as an exercice to the reader ;)

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


Sonia
Guest
 
Posts: n/a
#9: Jul 23 '05

re: atoi function



"Sonia" <smach02@hotmail.com> wrote in message
news:U6dPd.5176$a06.1477@bignews1.bellsouth.net...[color=blue]
> 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. Everywhere I look the usage is
> described, but I cannot actually view the code itself.
> Thanks
>[/color]

Thank You so much, that will get me started


Closed Thread