atoi function

S

Sonia

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
 
M

Mike Wahler

Sonia said:
Hi,
I've been using atoi for a while now,

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

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.
Can anyone give me simple efficient implementation of that function?

The simplest most efficient implementation is almost
certainly the one supplied with your compiler.
Or point me sowhere for reference.

See a book, newsgroup or website on algorithms.
Everywhere I look the usage is
described, but I cannot actually view the code itself.

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

-Mike
 
B

Brooke

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;
}
 
I

Ivan Vecerina

Sonia said:
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.
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;
}
 
M

Mike Wahler

Ivan Vecerina said:
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;
}

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
 
J

Jack Klein

Here is a start...

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.
#include <stdlib>
#define ASC_ZERO 48

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.
int Int2Str(int iNum, char* str)
{
char* Refstr = "0123456789";

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

static const char Refstr [] = "0123456789";

....but it is not needed at all.
int iRem;
int iCnt = 0;
int iflag = 0;

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

This can cause undefined behavior, because INT_MIN may be equal to
(-INT_MAX - 1), so -INT_MIN may overflow.
}

str[63 - iCnt] = 0;

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

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.
}while(iNum);

iCnt = 63 - iCnt - iflag;

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

return(iCnt);

'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++.
void main()

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

Owen Jacobson

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.

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
 
I

Ivan Vecerina

Mike Wahler said:
message


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()'?
:)

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 ;)
 
S

Sonia

Sonia said:
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

Thank You so much, that will get me started
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top