Previous Page
Next Page

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( )


Previous Page
Next Page