strtol function
strtol will convert a string to a 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 strtol(const char *sptr, char **endptr, int base);
Syntax: char String[]="ff"; /* string to convert */
int Base=16; /* Base 16 */
long int Ans; /* Result */
Ans = strtol(String, NULL, Base);
Notes
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.
- 0 - strtol will attempt to pick the base. Only Dec, Oct Hex supported.
- 2-31 - The base to use.
Examples:
Example program.
See also:
atoi String to integer conversion.
atof String to floating point conversion.
atol String to long integer conversion.
strtod String to double conversion.
strtoul String to unsigned long integer
conversion.
Conversion table.
Martin Leslie