Home >Backend Development >C#.Net Tutorial >How to convert string to number in c++
#c How to convert a string to a number?
C library function for converting strings into numbers
1. atoi
Function: Convert a string into an integer
Usage:int atoi(const char *nptr);
Detailed explanation: atoi is the abbreviation of English array to integer. atoi() will scan the parameter nptr string, and return zero if the first character is not a number or a sign, otherwise it will start type conversion, and then stop the conversion when it detects a non-number or the terminator /0, and return an integer. Parameters:
*nptr: String to be converted.
Return value:
int: converted integer number.
2. atol
Function: Convert a string to a long integer
Usage: long atol(const char *nptr) ;
Detailed explanation: atol() will scan the parameter nptr string, skip the preceding space characters, and do not start conversion until it encounters numbers or positive and negative symbols, and then encounters non-numbers or The conversion ends when the string ends ('/0') and the result is returned.
Parameters:
*nptr: The string to be converted.
Return value:
Long: The converted long integer.
3. atof
Function: Convert a string to a double-precision floating point number
Usage: double atof(const char * nptr);
Detailed explanation: atof() will scan the parameter nptr string, skip the preceding space characters, and do not start conversion until it encounters numbers or positive and negative symbols, and then encounters non- The conversion ends when the number or string ends ('/0') and the result is returned. The parameter nptr string can contain a plus or minus sign, a decimal point, or E(e) to represent the exponent part, such as 123.456 or 123e-2.
Parameters:
*nptr: The string to be converted.
Return value:
double: converted double-precision floating point number.
4. strtod
Function: Convert a string to a double-precision floating point value and report all remaining digits that cannot be converted
Usage: double strtod(const char *nptr,char **endptr);
Detailed explanation: strtod() will scan the parameter nptr string and skip the preceding space characters until it encounters The conversion starts when a number or positive or negative sign appears, and the conversion ends when a non-number or the end of the string ('/0') appears, and the result is returned. If endptr is not NULL, the character pointer in nptr that terminates when encountering unqualified conditions will be returned from endptr. The parameter nptr string can contain a plus or minus sign, a decimal point, or E(e) to represent the exponent part. Such as 123.456 or 123e-2.
Parameters:
*nptr: The string to be converted.
**endptr: If endptr is not NULL, the character pointer in nptr that terminates when encountering unqualified conditions will be returned from endptr.
Return value:
double: converted double-precision floating point number.
5. strtol
Function: Convert a string to a long value and report all remaining digits that cannot be converted
Usage:long int strtol(const char *nptr,char **endptr,int base);
Detailed explanation: This function will convert the parameter nptr string into a long integer according to the parameter base. The base parameter ranges from 2 to 36, or 0. The parameter base represents the base system used. If the base value is 10, decimal system is used. If the base value is 16, hexadecimal system is used. etc. When the base value is 0, decimal is used for conversion, but when encountering a leading character such as '0x', hexadecimal is used for conversion. When encountering a leading character of '0' instead of '0x', it will be converted. Use octal for conversion. At the beginning, strtol() will scan the parameter nptr string, skipping the preceding space characters, and will not start conversion until it encounters numbers or positive and negative symbols. It will end the conversion when it encounters non-numbers or the end of the string ('/0'). , and return the result. If the parameter endptr is not NULL, the character pointer in nptr that terminates due to unqualified conditions will be returned by endptr.
Parameters:
*nptr: The string to be converted.
**endptr: If endptr is not NULL, the character pointer in nptr that terminates when encountering unqualified conditions will be returned from endptr.
base: the base system used
Return value:
long int: the converted long integer.
6. strtoul
Function: Convert a string to an unsigned long value and report all remaining numbers that cannot be converted.
Usage: unsigned long int strtoul(const char *nptr,char **endptr,int base);
Detailed explanation: strtoul() will convert the parameter nptr string into an unsigned long integer according to the parameter base. The base parameter ranges from 2 to 36, or 0. The parameter base represents the base system used. If the base value is 10, decimal system is used. If the base value is 16, hexadecimal system is used. etc. When the base value is 0, decimal is used for conversion, but when it encounters a leading character such as '0x', hexadecimal is used for conversion. At the beginning, strtoul() will scan the parameter nptr string, skipping the preceding space string, and will not start conversion until it encounters a number or a positive or negative sign. Then it will end the conversion when it encounters a non-digit or the end of the string (''). and return the result. If the parameter endptr is not NULL, the character pointer in nptr that terminates when encountering unqualified conditions will be returned by endptr.
Parameters:
*nptr: The string to be converted.
**endptr: If endptr is not NULL, the character pointer in nptr that terminates when encountering unqualified conditions will be returned from endptr.
base: the base system used
Return value:
unsigned long int: the converted unsigned long integer.
The above is the detailed content of How to convert string to number in c++. For more information, please follow other related articles on the PHP Chinese website!