strtoul, strtoull


strtoul, strtoull

Converts a string into an unsigned long (or unsigned long long) integer value

 #include <stdlib.h> unsigned long strtoul ( const char * restrict s , char ** restrict endptr ,                        int base  ); unsigned long long strtoull (  const char  *  restrict s , char ** restrict endptr ,                              int base  );         (C99) 

The strtoul( ) function attempts to interpret the string addressed by its first pointer argument, s, as an integer numeric value, and returns the result with the type unsigned long. Otherwise, the strtoul( ) function works in the same way as strtol( ). strtoull( ) is similar, but returns unsigned long long.

If the resulting value is outside the range of the function's type, then the return value is ULONG_MAX (or ULLONG_MAX, for strtoull( )), and the errno variable is set to the value of ERANGE ("range error").

Example

This for loop uses stroul( ) to convert an IPv4 address from a dotted-decimal string to a 32-bit integer value:

 char dotted[ ] = "172.16.2.10", *ptr = dotted, *nextdot = NULL; unsigned long dest = 0; for ( i = 0; i < 4; i++) {   dest << 8;   dest += stroul( ptr, &nextdot, 10 );   ptr = nextdot + 1; } 

See Also

strtol( ), strtod( ), strtof( ), and strtold( ); wcstol( ) and wcstoul( ); strtoimax( ), strtoumax( ), wcstoimax( ), and wcstoumax( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net