Re: Converting large strings to numbers
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sat, 30 Apr 2005 00:14:02 GMT
"David Resnick" <lndresnick@xxxxxxxxx> writes:
> rou...@xxxxxxxxx wrote:
>> Without using errno, is there a portable way to detect if a string
>> number is not within the range 0 to 4294967295.
>>
>> Currently I am using strtoul. I can check the string for "-" as the
>> first character before calling strtoul, so I can figure out if the
>> value is less than 0.
>>
>> My issue arises when the number is greater than 4294967295 on
>> platforms where ULONG_MAX is 4294967295; in this case strtoul will
>> return ULONG_MAX and set errno to ERANGE. However, some platforms I
>> work on do not support errno. So there is no way for me to know
>> that the value was greater than 4294967295.
>>
>> Is there another way to do this ?
>
> Well, if strtoul returns ULONG_MAX, you could compare the string being
> converted to "4294967295". If it is NOT the same, you have overflowed.
Unless the string is "+4294967295", or "04294967295", or
"+0004294967295". That's if you use a "base" argument of 10; if you
set "base" to 0, a leading '0' implies octal, and you also have to
worry about "037777777777", "00037777777777", "0xffffffff",
"0X000FfFfFfFf", and so forth.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Converting large strings to numbers
- From: rouble
- Re: Converting large strings to numbers
- From: David Resnick
- Converting large strings to numbers
- Prev by Date: Re: How is strlen implemented?
- Next by Date: Re: How to remove and substitute characters within a string
- Previous by thread: Re: Converting large strings to numbers
- Next by thread: Re: Converting large strings to numbers
- Index(es):
Relevant Pages
|