STRTOL - convert string to long integer.

(ANSI Standard)


#include <stdlib.h>
li = strtol( s, p, base );


const char *s;
is the string to be converted into a long integer. This string may consist of any number of blanks and/or tabs, possibly followed by a sign, followed by a string of digits.
char **p;
points to a pointer that will be set to the character immediately following the long integer in the string "s". If no integer can be formed from "s", "strtol" makes "*p" point to the first character of "s". If "p" is the NULL pointer, this sort of action does not take place.
int base;
is the base for the number represented in the string. A "base" of zero indicates that the base should be determined from the leading digits of "s". The default is decimal, a leading '0' indicates octal, and a leading '0x' or '0X' indicates hexadecimal.
long li;
is the long integer obtained from "s".


#include <stdlib.h>
char *p, *s;
long li;
s = "20y";
li = strtol(s,&p,0);
 * At this point, "p" will point at the character 'y'
 * in the string constant, and "li" would have the
 * value 20
li = strtol(s,NULL,10);   /* assigns 20 to li */
li = strtol(s,NULL,8);   /* assigns 16 to li */


"strtol" converts the string "s" into a long integer. Conversion stops with the first character that cannot be part of such a number.

The string to be converted can contain the digits '0' to '9'. Depending on the base, the string can also contain letters representing digits greater than 9. The best known example is hexadecimal which uses '0' to '9' and 'A' to 'F' as digits. An upper case letter has the same value as a lower case one, so "abc" is the same number as "ABC".

Since you have 10 possible digits and 26 letters, the maximum value for the "base" argument is 36.

Copyright © 1996, Thinkage Ltd.