It is estimated that the first impression is atoi. In fact, it is not easy to use atoi when it encounters "0x1234", and I think it is best to use strtol and strtoul:
Http://huigezrx.blog.163.com/blog/static/3210165220102472843690/ from:
Strtol (converts a string to a growth integer)
Related functions
Atof, atoi, atol, strtodd, strtoul
Header file
# Include
Define functions
Long int strtol (const char * nptr, char ** endptr, int base );
Function Description
Strtol () converts the nptr parameter string 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 mode. 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, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. 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.
Return Value
Returns the converted long integer. Otherwise, the error code is returned and stored in errno.
Additional instructions
The conversion string specified by erange exceeds the valid range.
Example
/* Convert the string A, B, and C into numbers in hexadecimal notation of 10, 2, and 16 */
# Include
Main ()
{
Char A [] = "1000000000 ";
Char B [] = "1000000000 ";
Char C [] = "FFFF ";
Printf ("A = % d \ n", strtol (A, null, 10 ));
Printf ("B = % d \ n", strtol (B, null, 2 ));
Printf ("C = % d \ n", strtol (C, null, 16 ));
}
Run
A = 1000000000
B = 512
C = 65535.
Strtoul (convert a string to an unsigned long integer)
Related functions
Atof, atoi, atol, strtodd, strtol
Header file
# Include
Define functions
Unsigned long int strtoul (const char * nptr, char ** endptr, int base );
Function Description
Strtoul () converts the nptr parameter string to an unsigned long integer value based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. 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, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtoul () will scan the nptr parameter string and skip the leading space string until conversion starts when a number or positive or negative sign is encountered, 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.
Return Value
Returns the converted long integer. Otherwise, the error code is returned and stored in errno.
Additional instructions
The conversion string specified by erange exceeds the valid range.
Example
Refer to strtol ()