c++ instance method that converts a string to a number

  • 2020-06-23 01:40:15
  • OfStack

C++ library function that converts strings to Numbers

1, atoi

Work: Converts a 1 string to an integer

Method :int atoi(const char *nptr);

atoi is the abbreviation of array to integer. atoi() scans the parameter nptr string, returning zero if the first character is neither a number nor a sign, otherwise the conversion begins, and then stops when a non-number or terminator /0 is detected, returning the integer. Reference number:

*nptr: String to be transformed.

The return value:

int: Number of shadings after conversion.

2, atol

Work: Converts a 1 string to a long integer

Usage: long atol(const char *nptr);

atol() scans the parameter nptr string, skips the preceding space characters, does not convert until a number or sign is encountered, and does not convert until a non-number or string ends ('/0'), and returns the result.

Reference number:

*nptr: String to be transformed.

The return value:

long: Number of long shadings after conversion.

3, atof

Power: Converts a string to a double - precision floating - point number

Method: double atof(const char *nptr);

atof() scans the parameter nptr string, skips the preceding space characters, does not convert until a number or sign is encountered, and does not convert until a non-number or string ends ('/0'), and returns the result. The parameter nptr string may contain a plus or minus sign, decimal point, or E(e) to represent the exponential portion, such as 123.456 or 123ES75en-2.

Reference number:

*nptr: String to be transformed.

The return value:

double: Converted double-precision floating point number.

4, strtod

Power: Converts a string to a double-precision floating-point value and reports any remaining Numbers that cannot be converted

Usage: double strtod(const char *nptr,char **endptr);

strtod() scans the parameter nptr string, skips the preceding space characters, does not convert until a number or sign occurs, does not convert until a non-number or string ends ('/0'), and returns the result. If endptr is not NULL, a character pointer from nptr that terminates due to a non-condition is returned by endptr. The parameter nptr string may contain a plus or minus sign, a decimal point, or E(e) to represent the exponential part. Such as 123.456 or 123e-2.

Reference number:

*nptr: String to be converted.

**endptr: If endptr is not NULL, a character pointer from nptr that terminates after encountering a non-condition is returned by endptr.

The return value:

double: Converted double-precision floating point number.

5, strtol

Power: Converts a string to a long integer value and reports any remaining Numbers that cannot be converted

Method: long int strtol(const char *nptr,char **endptr,int base);

This function converts the parameter nptr string to a long integer based on the parameter base. The parameter base ranges from 2 to 36, or 0. The parameter base represents the hexadecimal method adopted. For example, if the value of base is 10, the hexadecimal system will be adopted; if the value of base is 16, the hexadecimal system will be adopted. When the base value is 0, the conversion is done in decimal, but when the '0x' preposition character is encountered, the conversion is done in decimal, and when the '0' preposition character is encountered, instead of '0x', the conversion is done in decimal. 1 strtol() scans the parameter nptr string, skips the preceding space character, does not convert until it encounters a number or a sign, and returns the result when it encounters a non-number or string ending ('/0'). If the parameter endptr is not NULL, the character pointer in nptr that terminates after encountering an unqualified condition is returned by endptr.

Reference number:

*nptr: String to be converted.

**endptr: If endptr is not NULL, a character pointer from nptr that terminates after encountering a non-condition is returned by endptr.

base: The base system adopted

The return value:

long int: Number of long reshapes after conversion.

6, strtoul

Power: Converts a string to an unsigned integer value and reports any remaining Numbers that cannot be converted.

Method: unsigned long int strtoul(const char *nptr,char **endptr,int base);

strtoul() converts the parameter nptr string to an unsigned long integer based on the parameter base. The parameter base ranges from 2 to 36, or 0. The parameter base represents the hexadecimal method adopted. For example, if the value of base is 10, the hexadecimal number will be adopted; if the value of base is 16, the hexadecimal number will be adopted. When the value of base is 0, the conversion is done in decimal, but in case of a preposition character such as '0x', the conversion is done in decimal. 1 strtoul() scans the parameter nptr string, skips the preceding space string, does not begin the conversion until it encounters a number or a sign, and when it encounters a non-number or string end ('), ends the conversion and returns the result. If the parameter endptr is not NULL, the character pointer in nptr that terminates after encountering a non-condition is returned by endptr.

Reference number:

*nptr: String to be transformed.

**endptr: If endptr is not NULL, a character pointer from nptr that terminates due to a non-condition is returned by endptr.

base: Base mode adopted

The return value:

unsigned long int: Number of unsigned long reshapes after conversion.

That's how c++ converts strings to Numbers. Thanks for learning and supporting this site.


Related articles: