You can use the itoa function to convert the value of a variable into a binary string, and then use the output function to output it.
Usage: char * itoa (int value, char * string, int root);
Itoa is the abbreviation of English integer to array (converting int integer into a string and saving the value in the array string).
Parameter: value: integer to be converted.
Radix: represents radix, that is, the value is converted into a number represented by radix, ranging from 2 to 36, for example, 10 means 10, 16 means 16.
* string: Save the converted string.
Return value: char *: points to the generated string, the same as *string.
Note: the header file of this function is "stdlib.h"
The following functions can convert strings into numbers:
-
Function name? Work? use
-
Atof () converts a string to 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 to a double-precision floating-point value and reports all remaining digits that cannot be converted.
Strtol () converts a string to 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.
-
When converting a string to a number, it may cause an overflow. If you use a function like strtoul (), you can check this overflow error. Please look at the following example:
The code is as follows:
# include & ltstdio。 h & gt
# include & ltstdlib。 h & gt
# include & lt limit. h & gt
Void main (invalid);
Invalid master (invalid)
{
char * str = " 123456789 10 1 12 13 14 15 16 17 18 1920 ";
Unsigned long integer;
Char * remaining;
Num = strtoul(str,&,10);
Printf ("original string: %s\n", str);
Printf ("converted number:% 1u\n", num);
Printf ("remaining characters: %s\n", remaining);
}