C language string to numeric value

Source: Internet
Author: User
Tags float number ranges
It is often used to convert strings and numbers. It is easy to collect and sort them. Then you can use atof (convert string to float type) atoi (convert string to integer type) atol (convert string to integer type) strtol (convert string to floating point number) strtol (convert string to integer) strtoul (convert string to unsigned long integer) toascii (convert integer to valid ASCII characters) toupper (convert lowercase letters to uppercase letters) tolower (convert uppercase letters to lowercase letters) atof (converts a string to a float number) related functions: atoi, atol, strtodd, strtol, strtoul header file: # include <stdlib. h> define the function: Double atof (const char * nptr); Function Description: atof () scans the nptr parameter string and skips the leading space character, conversion is not started until a number or a positive or negative sign is met, but the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponential part, such as 123.456 or 123e-2. Return Value: return the number of Float points after conversion. Note: The atof () is the same as the use of strtodd (nptr, (char **) null. Example:

/* Convert string a and string B into numbers and add them together */

# Include <stdlib. h> main () {char * A = "-100.23"; char * B = "200e-2"; float C; C = atof (A) + atof (B ); printf ("C = %. 2f \ n ", c) ;}execute c =-98.23

 

Atoi (converts a string to an integer) related functions: atof, atol, atrtodd, strtol, strtoul header file: # include <stdlib. h> define the function: int atoi (const char * nptr). Function Description: atoi () scans the nptr parameter string and skips the leading space character, conversion is not started until a number or a positive or negative sign is met, but the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. Return Value: the number of converted integers. Note: atoi () and use strtol (nptr, (char **) null, 10); the results are the same. Example:

/* Convert string a and string B into numbers and add them together */

# Include <stdlib. h> Mian () {char a [] = "-100"; char B [] = "456"; int C; C = atoi (A) + atoi (B ); printf (C = % d \ n ", c);} execute c = 356

 

Atol (converts string to integer) related functions: atof, atoi, strtodd, strtol, strtoul header file: # include <stdlib. h> define the function: Long atol (const char * nptr); Function Description: atol () scans the nptr parameter string and skips the leading space character, conversion is not started until a number or a positive or negative sign is met, but the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. Return Value: return the number of converted long integers. Additional instructions: atol () and use strtol (nptr, (char **) null, 10); the results are the same. Example:

/* Convert string a and string B into numbers and add them together */

# Include <stdlib. h> main () {char a [] = "1000000000"; char B [] = "234567890"; long C; C = atol (A) + atol (B ); printf ("C = % d \ n", c);} execute c = 1234567890

 

Gcvt (convert the float number to a string and rounding it out) related functions: ECVT, fcvt, sprintf header file: # include <stdlib. h> defining function: char * gcvt (double number, size_t ndigits, char * BUF); Function Description: gcvt () is used to convert the parameter number into an ASCII string, the ndigits parameter indicates the number of digits displayed. Gcvt () differs from ECVT () and fcvt () in that the string converted by gcvt () contains the decimal point or positive or negative sign. If the conversion is successful, the converted string will be placed in the space specified by the Buf pointer. Return Value: returns a string pointer. This address is the Buf pointer. Example:

# Include <stdlib. h>

Main () {double A = 123.45; Double B =-1234.56; char * PTR; int decpt, sign; gcvt (A, 5, PTR ); printf ("A value = % s \ n", PTR); PTR = gcvt (B, 6, PTR); printf ("B value = % s \ n ", PTR);} execute a value = 123.45 B value =-1234.56

 

Related functions: atoi, atol, strtodd, strtol, and strtoul header file: # include <stdlib. h> define the function: Double strtop (const char * nptr, char ** endptr); Function Description: strtop () scans the nptr parameter string and skips the leading space character, conversion is not started until a number or positive or negative sign is met. The conversion ends only when a non-number or string ends ('\ 0') and the result is returned. If the endptr value is not null, the character pointer in the nptr that is terminated when an exception occurs is returned by the endptr. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponent part. Such as 123.456 or 123e-2. Return Value: return the number of Float points after conversion. For more information, see atof (). Example:

/* Convert the string A, B, and C into numbers in hexadecimal notation of 10, 2, and 16 */

# Include <stdlib. h> Mian () {char a [] = "1000000000"; char B [] = "1000000000"; char C [] = "ffff "; printf ("A = % d \ n", strcto (A, null, 10); printf ("B = % d \ n", strcto (B, null, 2); printf ("C = % d \ n", strtodd (C, null, 16);} execute a = 1000000000 B = 512 c = 65535

 

Strtol (convert string to integer number) related functions: atof, atoi, atol, strtodd, strtoul header file: # include <stdlib. h> defined function: Long int strtol (const char * nptr, char ** endptr, int base); Function Description: strtol () the nptr string is converted to the Growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it starts to convert in case of a number or positive or negative sign, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not null, the character pointer in the nptr that is terminated due to an exception is returned by the endptr. Returns the number of long integers after conversion. Otherwise, an error is returned. Code Stored in errno. Note: The conversion string specified by erange is out of the valid range. Example:

/* Convert the string A, B, and C into numbers in hexadecimal notation of 10, 2, and 16 */

# Include <stdlib. h> main () {char a [] = "1000000000"; char B [] = "1000000000"; char C [] = "FFFF "; printf ("A = % d \ n", strtol (A, null, 10); printf ("B = % d \ n", strtol (B, null, 2); printf ("C = % d \ n", strtol (C, null, 16);} execute a = 1000000000 B = 512 c = 65535

 

Strtoul (convert string to unsigned long integer number) related functions: atof, atoi, atol, strtodd, strtol header file: # include <stdlib. h> define the function: Unsigned long int strtoul (const char * nptr, char ** endptr, int base); Function Description: strtoul () the nptr string is converted to an unsigned long integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtoul () will scan the nptr parameter string and skip the leading space string until conversion starts when a number or positive or negative sign is encountered, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not null, the character needle in the nptr that is terminated due to an exception is returned by the endptr. Return Value: return the converted long integer. Otherwise, the error code is returned and stored in errno. Note: The conversion string specified by erange is out of the valid range. Example: see strtol ()

 

Toascii (convert integer to valid ASCII characters) related functions: isascii, toupper, tolower header file: # include <ctype. h> defined function: int toascii (INT c) Function Description: toascii () converts parameter C to a 7-bit unsigned char value, and the eighth digit is cleared, this character is converted into ASCII characters. Return Value: returns the converted ASCII code. Example:

# Include <stdlib. h>

Main () {int A = 217; char B; printf ("Before toascii (): A value = % d (% C) \ n", a, ); B = toascii (a); printf ("after toascii (): A value = % d (% C) \ n", B, B);} execute before toascii (): A value = 217 () after toascii (): A value = 89 (y)

 

Tolower (converts uppercase letters to lowercase letters) related functions: isalpha, toupper header file: # include <stdlib. h> define the function: int tolower (INT c). Function Description: If the parameter C is an upper-case letter, the corresponding lower-case letter is returned. Return Value: return the converted lowercase letter. If no conversion is required, the C value is returned. Example:

/* Convert uppercase letters in the S string to lowercase letters */

# Include <ctype. h> main () {char s [] = "abcdefgh12345 ;! # $ "; Int I; printf (" Before tolower (): % s \ n ", S); for (I = 0; I <sizeof (s ); I ++) s [I] = tolower (s [I]); printf ("After tolower (): % s \ n", S);} execute before tolower (): abcdefgh12345 ;! # $ After tolower (): abcdefgh12345 ;! # $

 

Toupper (converts lowercase letters to uppercase letters) related functions: isalpha, tolower header file: # include <ctype. h> defines the function: int toupper (INT c). Function Description: If the parameter C is a lowercase letter, the uppercase letter of the ing is returned. Return Value: return the converted uppercase letters. If no conversion is required, the C value is returned. Example:

/* Convert lowercase letters in the S string to uppercase letters */

# Include <ctype. h> main () {char s [] = "abcdefgh12345 ;! # $ "; Int I; printf (" Before toupper (): % s \ n ", S); for (I = 0; I <sizeof (s ); I ++) s [I] = toupper (s [I]); printf ("after toupper (): % s \ n", S);} execute before toupper (): abcdefgh12345 ;! # $ After toupper (): abcdefgh12345 ;! # $

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.