while working on an application that requires converting strings to long numbers the atol() and strtol() could not convert any string larger than 9 digits correctly.
strtol would prompt a number that has nothing to do with the string, while atol results in a negative number (overflow).
any idea why this is happening and how can i fix it?
It sounds like a long
on your system is a 32-bit value. That means any unsigned number above 4,294,967,295 won't convert correctly, and neither will signed numbers above 2,147,483,647 or below 2,147,483,648.
In general, an n-bit representation can represent signed numbers in the range [-2n-1,-2n-1) or unsigned numbers in the range [0,2n).
The wikipedia article Computer numbering formats is probably a good jumping off point to learn more about this behaviour.
The reason you see different results from atol()
and strtol()
is because they have different error handling characteristics. From the strtol()
man page:
The
strtol()
function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs,strtol()
returnsLONG_MIN
. If an overflow occurs,strtol()
returnsLONG_MAX
. In both cases,errno
is set toERANGE
.
And from the atol()
man page:
The
atoi()
function converts the initial portion of the string pointed to bynptr
to int. The behaviour is the same asstrtol(nptr, (char **)NULL, 10);
except that
atoi()
does not detect errors.The
atol()
andatoll()
functions behave the same asatoi()
, except that they convert the initial portion of the string to their return type oflong
orlong long
.
This probably has to do with the size of a long
on your platform. On 32-bit platforms, the largest value that fits in a long
is 2147483647 (2^31-1), so anything larger than that simply won't fit. Use long long
and strtoll
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With