Syntax
#include <stdlib.h> unsigned long int strtoul(const char *string1, char **string2, int base);Description
strtoul converts a character string to an unsigned long integer value. The input string1 is a sequence of characters that can be interpreted as a numerical value of the type unsigned long int. strtoul stops reading the string at the first character that it cannot recognize as part of a number. This character can be the first numeric character greater than or equal to the base. strtoul sets string2 to point to the resulting output string if a conversion is performed, and provided that string2 is not a NULL pointer.
When you use strtoul, string1 should point to a string with the following form:
┌──────────────────────────────────────────────────────────────────────────────┐
│                                        │
│ >>──┬─────────────┬──┬───┬──┬────┬──┬────────┬──><       
     │
│   └─white-space─┘  ├─+─┤  ├─0──┤  └─digits─┘                │
│            └─┴─┘  ├─0x─┤                      │
│               └─0X─┘                      │
│                                        │
└──────────────────────────────────────────────────────────────────────────────┘
If base is in the range of 2 through 36, it becomes the base of the number. If base is 0, the prefix determines the base (8, 16, or 10): the prefix 0 means base 8 (octal); the prefix 0x or 0X means base 16 (hexadecimal); using any other digit without a prefix means decimal.
strtoul returns the value represented in the string, or 0 if no conversion could be performed. For an overflow, strtoul returns ULONG_MAX and sets errno to ERANGE. If base is not a valid number, strtoul sets errno to EDOM.
This example converts the string to an unsigned long value. It prints out the converted value and the substring that stopped the conversion.
#include <stdio.h>
#include <stdlib.h>
#define  BASE          2
int main(void)
{
   char *string,*stopstring;
   unsigned long ul;
   string = "1000e13 e";
   printf("string = %s\n", string);
   ul = strtoul(string, &stopstring, BASE);
   printf("   strtoul = %ld (base %d)\n", ul, BASE);
   printf("   Stopped scan at %s\n\n", stopstring);
   return 0;
   /****************************************************************************
      The output should be:
      string = 1000e13 e
         strtoul = 8 (base 2)
         Stopped scan at e13 e
   ****************************************************************************/
}
Related Information