Convert a number to a string
Example 1: use the library function to convert numbers into strings.
Test site: Use of the C library function to convert numbers into strings.
Frequency:★★★
Analysis
The C language provides several standard library functions that can convert numbers of any type (integer, long integer, floating point type, etc.) into strings. The following describes the methods and descriptions of each function.
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 the float number to a string and returns the rounded value.
ECVT (): converts a double-precision floating point value to a string without the decimal point.
Fcvt (): the specified number of digits is the conversion precision, and the rest are the same as ECVT ().
You can also use the sprintf series functions to convert numbers into strings, which is slower than the ITOA () series functions. The following program demonstrates how to use the ITOA () function and gcvt () function:
1 # include <stdio. h> 2 # include <stdlib. h> 3 4 int main () 5 { 6 int num_int = 435; 7 double num_double = 435.10f; 8 char str_int [30]; 9 char str_double [30]; 10 11 ITOA (num_int, str_int, 10); // convert the integer num_int into a string str_int 12 gcvt (num_double, 8, str_double); // float Point num_double to string str_double 13 14 printf ("str_int: % s/n", str_int ); 15 printf ("str_double: % s/n", str_double ); 16 17 return 0; 18} |
Program output result:
1 str_int: 435 2 str_double: 435.10001 |
The 10 parameter in line 11th of the Code indicates the conversion in decimal format. The conversion result is "435". If the conversion is based on binary type, the result is "1101110011 ".
The parameter 8 in line 12th of the Code indicates the exact number of digits. The result is "435.10001 ".
Answer
You can use atoi functions to convert numbers into strings.
Example 2: Do not use the library function to convert integers into strings.
Test site: convert numbers into strings and understand the relevant ASCII code.
Frequency:★★★★
Analysis
If you do not use database functions such as atoi or sprintf, you can convert the numbers on each integer to "0" to char type and coexist in the character array. However, you must use the reverse string method. As shown in the following program:
1 # include <iostream> 2 using namespace STD; 3 4 void int2str (int n, char * Str) 5 { 6 char Buf [10] = ""; 7 int I = 0; 8 int Len = 0; 9 int temp = n <0? -N: N; // temp is the absolute value of N. 10 11 if (STR = NULL) 12 { 13 return; 14} 15 while (temp) 16 { 17 Buf [I ++] = (TEMP % 10) + '0'; // Save the number of each bit of temp to Buf 18 temp = temp/10; 19} 20 21 Len = n <0? ++ I: I; // if n is a negative number, one more bit is required to store the negative number. 22 STR [I] = 0; // The end is 0. 23 while (1) 24 { 25 I --; 26 if (BUF [len-i-1] = 0) 27 { 28 break; 29} 30 STR [I] = Buf [len-i-1]; // copy the characters in the Buf array to a string 31} 32 if (I = 0) 33 { 34 STR [I] = '-'; // if it is a negative number, add a negative number. 35} 36} 37 38 int main () 39 { 40 int nnum; 41 char P [10]; 42 43 cout <"Please input an integer :"; 44 CIN> nnum; 45 cout <"output :"; 46 int2str (nnum, P); // converts an integer to a string. 47 cout <p <Endl; 48 49 return 0; 50} |
The int2str function in the program converts the int type to the string type. The int2str function is tested in line 2 of the code. The execution result of the program is as follows:
Please input an integer: 1234 Output: 1234 |
If the input is a negative number, the program execution result is as follows:
Please input an integer: -1234 Output: -1234 |
Next, we will analyze the implementation of the int2str function.
Line 2 of the Code assigns the absolute value of parameter n to temp, and then uses temp to calculate the Integers of all bits. This ensures that the remainder is not affected in the case of negative numbers.
Code 11th ~ Row 3 judges the validity of str. STR is not null.
Code 15th ~ In the while loop of the first row, each bit of N is stored in a local array Buf, which is in the opposite order of integers. For example, if n is an integer of 123 456 and the while loop ends, the Buf should be "654 321 ".
Line 2 of the code calculates the length of the converted string Len. If it is a negative number, the length should be increased by 1.
Code 22nd ~ Row 3 copies the non-0 elements in the array Buf to the memory directed by the STR parameter. If n is a negative number, the first memory to which STR points stores the negative number.