Re: Converting large strings to numbers
- From: Lawrence Kirby <lknews@xxxxxxxxxxxxxxx>
- Date: Fri, 29 Apr 2005 18:03:35 +0100
On Thu, 28 Apr 2005 16:42:00 +0000, Walter Roberson wrote:
> In article <1114705952.168003.22850@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
> <rouble@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.
>
> Stringify ULONG_MAX
Ok as long as you mean convert the value to a string with, say, sprintf()
and not by preprocessor tricks on the ULONG_MAX macro itself.
> and do a strcmp() of the source number against
> that value. If the source number is all digits then strcmp() of the
> decimal representations is equivilent to a numeric comparison.
Unless there are leading zeros.
> Even in
> odd character sets, because of the C standard's specifi rules about the
> representation of the digits.
It would still work even if the digits weren't contiguous in the character
set.
Lawrence
.
- References:
- Converting large strings to numbers
- From: rouble
- Re: Converting large strings to numbers
- From: Walter Roberson
- Converting large strings to numbers
- Prev by Date: Re: Count maximum contiguous set bits in an integer .
- Next by Date: Re: How is strlen implemented?
- Previous by thread: Re: Converting large strings to numbers
- Next by thread: Re: Converting large strings to numbers
- Index(es):
Relevant Pages
|