When writing a program, we often encounter the problem of converting strings and numbers. Here is a small summary.
There are many conversion methods for strings and numbers. Different development environments can be divided into C/C ++/mfc. Of course, there is an inclusion relationship between the three, the methods implemented in the C development environment can be used in the C ++ development environment, while the methods used in the C ++ development environment can also be used in the MFC. Because c ++/mfc uses class templates, cstring classes, and string classes, there are many solutions to these problems. Therefore, we mainly introduce the methods in the C development environment.
1. convert a string to a number
C Development Environment: mainly uses the atof atoi atol functions. Example and usage:
Function Name: atof
Function: converts a string to a floating point number.
Usage: Double atof (const char * nptr );
Program example:
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Float F;
Char * STR = "12345.67 ";
F = atof (STR );
Printf ("string = % s float = % F \ n", STR, F );
Return 0;
}
Function Name: atoi
Function: converts a string to an integer.
Usage: int atoi (const char * nptr );
Program example:
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Int N;
Char * STR = "12345.67 ";
N = atoi (STR );
Printf ("string = % s integer = % d \ n", STR, N );
Return 0;
}
Function Name: atol
Function: converts a string to an integer.
Usage: Long atol (const char * nptr );
Program example:
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Long L;
Char * STR = "98765432 ";
L = atol (lstr );
Printf ("string = % s integer = % LD \ n", STR, L );
Return (0 );
}
Of course, you can also write functions for implementation by yourself. Generally, programmers like this question during interviews. The functions and the corresponding Code are as follows:
Int str2int (constchar * Str)
{
Int itemp = 0;
Const char * PTR = STR; // PTR saves the start of the STR string
// If the first character is a plus or minus sign, move it to the next character
If (* STR = '-' | * STR = '+ ')
{
STR ++;
}
While (* Str! = 0)
{
// Exit the loop if the current character is not a number
If (* STR <'0') | (* STR> '9 '))
{
Break;
}
// If the current character is a number, the value is calculated and moved to the next character.
Itemp = itemp * 10 + (* str-'0 ');
STR ++;
}
// If the string starts with "-", it is converted to the opposite number.
If (* PTR = '-')
{
Itemp =-itemp;
}
Return temp;
}
2. convert numbers to strings (C Development Environment)
The problem here is relatively simple. You can use the sprintf format input function to solve the problem, or you can use the ITOA () Library Function in C ().
Examples:
Char
STR [10];
Sprintf (STR,
"% D", 99 );
Function Name: ITOA
Function: converts an integer to a string.
Usage: char * ITOA (INT value, char * string, int Radix );
Program example:
# Include <stdlib. h>
# Include <stdio. h>
Int main (void)
{
Int number = 12345;
Char string [25];
ITOA (number, String, 10 );
Printf ("integer = % d string = % s \ n", number, string );
Return 0;