The prototype of the strtol () function is:
Long int strtol (const char * nptr, char ** endptr, int base );
Function Description
This function converts the nptr parameter to the Growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base method. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, 10 is used for conversion, however, if the '0x 'prefix is used for hexadecimal conversion, and if the '0' prefix is not '0x', the '8' prefix is used for conversion. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it starts to convert in case of a number or positive or negative sign, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not null, the character pointer in the nptr that is terminated due to an exception is returned by the endptr.
Strtol is an enhanced version of atoi
This is mainly reflected in the following aspects:
1. It not only recognizes decimal integers, but also recognizes other hexadecimal integers, depending on the base parameter, such as strtol ("0xdeadbee ~~ ", Null, 16) returns the 0xdeadbee value, strtol (" 0777 ~~ ", Null, 8) returns the value of 0777.
2. endptr is an outgoing parameter. The function returns the first character that is not recognized. For example, char * Pos; strtol ("123abc", & Pos, 10);, strtol returns 123, and Pos points to the letter A in the string. If no identifiable integer exists at the beginning of the string, for example, char * Pos; strtol ("abcabc", & Pos, 10);, strtol returns 0, and Pos points to the beginning of the string, this can be used to determine the case of this error, which is not handled by atoi.
3. If the integer in the string exceeds the long int value (overflow or underflow), strtol returns the maximum (or minimum) integer it can represent and sets errno to erange, for example, strtol ("0xdeadbeef ~~ ", Null, 16) returns 0x7fffffff and sets errno to erange
In most cases, the endptr is set to null, that is, no invalid string is returned.
The following are examples:
------------------------------------------------------
Char buffer [20] = "10379 Cend $3 ";
Char * stop;
Printf ("% d \ n", strtol (buffer, & stop, 2 ));
Printf ("% s \ n", stop );
Output result:
2
379 Cend $3
-------------------------------------------------------
Char buffer [20] = "10379 Cend $3 ";
Char * stop;
Printf ("% d \ n", strtol (buffer, & stop, 8 ));
Printf ("% s \ n", stop );
Output result:
543
9 Cend $3
--------------------------------------------------------
Char buffer [20] = "10379 Cend $3 ";
Char * stop;
Printf ("% d \ n", strtol (buffer, & stop, 10 ));
Printf ("% s \ n", stop );
Output result:
10379
Cend $3
-------------------------------------------------------
Char buffer [20] = "10379 Cend $3 ";
Char * stop;
Printf ("% d \ n", strtol (buffer, & stop, 16 ));
Printf ("% s \ n", stop );
Output result:
17005006
Nd $3
In addition, if the base is 0 and the string does not start with 0x (or 0x), it is converted in decimal format. If the base is 0 or 16 and the string starts with 0x (or 0x), x (or X) is ignored and the string is converted in hexadecimal format. If base is not equal to 0 and 16 and the string starts with 0x (or 0x), X is considered invalid.
For example:
-------------------------------------------------------
Char buffer [20] = "0x31da6c ";
Char * stop;
Printf ("% d \ n", strtol (buffer, & stop, 0 ));
Printf ("% s \ n", stop );
Output result (stop is empty ):
3267180
-------------------------------------------------------
Char buffer [20] = "0x31da6c ";
Char * stop;
Printf ("% d \ n", strtol (buffer, & stop, 13 ));
Printf ("% s \ n", stop );
Output result:
0
0x31da6c
-------------------------------------------------------
Finally, it should be noted that for the string to which nptr points, the spaces at the beginning and end of the string are ignored, and the spaces in the middle of the string are considered invalid characters.
For example:
-------------------------------------------------------
Char buffer_1 [20] = "10366c ";
Char buffer_2 [20] = "10366c ";
Char buffer_3 [20] = "10 379c ";
Printf ("% d \ n", strtol (buffer_1, null, 0 ));
Printf ("% d \ n", strtol (buffer_2, null, 0 ));
Printf ("% d \ n", strtol (buffer_3, null, 0 ));
Output result:
10379
10379
10