Re: Converting large strings to numbers



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

.



Relevant Pages

  • Re: inputting the ephemerides (SOLUTION!)
    ... Defining the rules for field parsing has to match all the ... these are placed in the character string CWK). ... string stops the parsing and GOs to statement 7. ... Here the string CWK of sign, digits and a decimal point if present are ...
    (comp.lang.fortran)
  • Re: user defined function that converts string to float
    ... > I need user defined function that converts string to float in c. ... initial, possibly empty, sequence of white-space characters (as ... point character, then an optional exponent part as defined in ... then a nonempty sequence of hexadecimal digits ...
    (comp.lang.c)
  • Re: inputting the ephemerides (SOLUTION!)
    ... Defining the rules for field parsing has to match all the ... these are placed in the character string CWK). ... string stops the parsing and GOs to statement 7. ... Here the string CWK of sign, digits and a decimal point if present are ...
    (comp.lang.fortran)
  • Re: Newbie - itoa implementation
    ... Now we need to convert from number of binary digits to number of decimal digits. ... Since we need space for the null character to terminate the string, ...
    (comp.lang.c)
  • Re: strip numeric characters from a text field
    ... the string, one character at a time, looking for digits and modifying the ... string to leave each one out. ...
    (microsoft.public.access.queries)