#include <stdlib.h>
The 1.int/float to STRING/ARRAY:C language provides several standard library functions that convert numbers of any type (integer, long integer, floating-point type, etc.) to strings, and the methods and descriptions of each function are listed below.
Itoa (): Converts an integer value to a string.
Ltoa (): Converts a long integer value to a string.
Ultoa (): Converts an unsigned long integer value to a string.
GCVT (): Converts floating-point numbers to strings, rounded.
ECVT (): Converts a double-precision floating-point value to a string that does not contain a decimal point in the transformation result.
FCVT (): The specified number of digits is the conversion precision and the remainder is ECVT ().
In addition, you can use the SPRINTF series function to convert numbers to strings that run slower than itoa () series functions
2. String/array to Int/float
C + + language provides several standard library functions that can convert strings to any type (integer, long, floating-point, and so on). Atof (): Converts a string to a double-precision floating-point value.
Atoi (): Converts a string to an integer value.
ATOL (): Converts a string to 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 value and reports all remaining digits that cannot be converted.
The following is an example of converting an integer to a string using the Itoa () function:
# include <stdio.h>
# include <stdlib.h>
void Main (void)
{
int num = 100;
Char str[25];
ITOA (num, str, 10);
printf (' The number ' num ' is%d and the string ' str ' is%s. \ n ',
num, str);
}
The itoa () function has 3 parameters: The first argument is the number to convert, the second parameter is the target string to write the transformation result, and the third parameter is the cardinality used to transfer the number. In the example above, the conversion base is 10. 10: decimal; 2: Binary ...
Itoa is not a standard C function, it is Windows specific, if you want to write Cross-platform program, please use sprintf. is extended under the Windows platform, with sprintf in the standard library, more powerful than this, with a similar usage to printf:
Char str[255];
sprintf (str, "%x", 100); A string that converts 100 to a 16-binary representation.