strtoul function


strtoul will convert a string to an unsigned long integer. An important feature of this function is the ability to accept data in various number bases and convert to decimal. If you are just working with decimal numbers, atoi is probably an easer function to use.


Library:   stdlib.h

Prototype: long int strtoul(const char *sptr, char **endptr, int base);

Syntax:	   char string1[]="ff";			/* string to convert	*/
	   int  base=16;			/* Base 16		*/
           unsigned long int ans;		/* Result		*/

           ans = strtoul(string, NULL, 16);

Notes

The first argument must not contain a + or -.

The second argument (char **endptr) seems to be a waste of space! If it is set to NULL, STRTOL seems to work its way down the string until it finds an invalid character and then stops. All valid chars read are then converted if the string starts with an invalid character the function returns ZERO (0).

The Third argument (base) can have a value of 0 or 2-32.


Examples

example program (actually the strtol example, but its near enough).


See also:

atoi String to integer conversion.
atof String to floating point conversion.
atol String to long integer conversion.
strtod String to double conversion.
strtol String to long integer conversion.


Top Master Index Keywords Functions


Martin Leslie