Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How does C language convert character strings into numbers that can be used for calculation?
How does C language convert character strings into numbers that can be used for calculation?
(1) Using the string conversion function in the standard function library stdlib, you can convert various types of strings into corresponding types of numbers.

(2) Use the sscanf function in the input-output standard function library stdio.

Use sscanf function in standard library (where format control characters are used in the same way as scanf, %d reads decimal integer, %x reads 16 hexadecimal integer). For example: char s [] = {"9228397673b1c0801e448b0bd44dae6e"}; int a,b,c,d; Sscanf(s, "%d% x% x", & i, & ampb & amp;; c,& ampd); The premise is to ensure that the string format is consistent with the format controller, otherwise the consequences are uncertain. In addition, bd44dae6e here exceeds the range of int on 32-bit platform. If you want to read it correctly, you need to use long long, and the corresponding format control characters are changed to Lx or llx.

Atof () converts a string into a double-precision floating-point value; Atoi () converts a string into an integer value; Atol () converts a string into a long integer value; Strtod () converts a string into a double-precision floating-point value and reports all remaining digits that cannot be converted; Strtol () converts a string into a long integer value and reports all remaining digits that cannot be converted; Strtoul () converts a string to an unsigned long integer and reports all remaining digits that cannot be converted.