Math Function numeric string conversion case-insensitive Conversion

Source: Internet
Author: User
Atof (convert string to float type) 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 completed until a number or a positive or negative sign is met, but when a non-number or string ends (''), the conversion is completed 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 the number of floating point types after conversion. The additional description atof () is the same as the use of strtodd (nptr, (char **) null. Example/* convert string a and string B into numbers and add them */# include <stdlib. h> main () {char * A = "-100.23"; char * B = "200e-2"; float C; C = atof (A) + atof (B ); printf ("C = %. 2f \ n ", c );} run c =-98.23 /////////////////////////////////// //// // atoi (converts a string to an integer type) number) header file # include <stdlib. h> defines the function int atoi (const char * nptr). The function indicates that 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 ('') and the result is returned. Return the number of converted integers. Additional description atoi () and use strtol (nptr, (char **) null, 10); the result is the same. Example/* convert string a and string B into numbers and add them */# include <stdlib. h> Mian () {char a [] = "-100"; char B [] = "456"; int C; C = atoi (A) + atoi (B ); printf (C = % d \ n ", c );} run c = 356 //////////////////////////////////// /// // atol (converts a string to an integer) header file # include <stdlib. h> define the function long atol (const char * nptr). The function indicates that 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 ('') and the result is returned. Return the number of converted long integers. Additional description 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 */# include <stdlib. h> main () {char a [] = "1000000000"; char B [] = "234567890"; long C; C = atol (A) + atol (B ); printf ("C = % d \ n", c );} run c = 1234567890 //////////////////////////////////// ///////////////// // gcvt (convert the floating point number to a string, returns the rounded value.) the related functions include ECVT, fcvt, and sprintf header file # include <stdlib. h> define the function char * gcvt (double number, size_t ndigits, char * BUF). The function number indicates that gcvt () is used to convert the parameter number into an ASCII string, the ndigits parameter indicates the number of digits displayed. The difference between gcvt () and ECVT () is that the string converted by gcvt () contains a decimal point or a positive or negative sign. If the conversion is successful, the converted string will be placed in the space specified by the Buf pointer. The Return Value Returns a string pointer, which is the Buf pointer. Additional instructions # 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 );} run a value = 123.45b value =-1234.56 /////////////////////////////// //////////////////////////////////////// strtodd (convert a string to a floating point number) header file # include <stdlib. h> define the function Double strtop (const char * nptr, char ** endptr). The number of functions indicates that strtop () will scan the nptr string parameter., Skip the leading space character, and start conversion only when a number or positive or negative sign is encountered. The conversion ends when a non-number or string ends (''), 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 the number of floating point types 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 ));} run a = 000000000b = 512c = 65535 //////////////////////////////// //////////////////////////////////////// ////////// strtol (convert the string to the integer number) related functions: atof, atoi, atol, strtodd, and strtoul header file # inclu De <stdlib. h> define the 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, skips the leading space character, and starts conversion only when a number or positive or negative sign is encountered, and then ends when a non-number or string ends ('') end the conversion and return the result. 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 that 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 ));} run a = 000000000b = 512c = 65535 //////////////////////////////// //// // strtoul (convert the string to an unsigned long integer) related functions: atof, atoi, atol, strtodd, and strtol header file # include <stdlib. h> define the function unsig Ned 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 () scans the nptr parameter string, skips the leading space string, and starts conversion only when a number or positive or negative sign is encountered, and then ends when a non-number or string ends ('') end the conversion and return the result. 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. Return the number of converted long integers. Otherwise, the error code is returned and stored in errno. Note that the conversion string specified by erange is out of the valid range. For an example, refer to the related functions isascii, toupper, and tolower header file # include <ctype. h> the int toascii (INT c) function indicates that toascii () converts the parameter C to a 7-bit unsigned char value, and the eighth digit is cleared, this character is converted into ASCII characters. The 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) function isalpha, toupper header file # include <stdlib. h> defines the int tolower (INT c) function. It indicates that if the parameter C is an upper-case letter, the corresponding lower-case letter is returned. Return the converted lowercase letter. If no conversion is required, the C value is returned. Additional instructions/* 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 = tolower (s); printf ("After tolower (): % s \ n", S) ;}execute before tolower (): abcdefgh12345 ;! # $ After tolower (): abcdefgh12345 ;! # $ Toupper (converts lowercase letters to uppercase letters) functions related to isalpha and tolower header files # include <ctype. h> defines the int toupper (INT c) function. It indicates that if the parameter C is a lowercase letter, the uppercase letter of the ing is returned. Return the converted uppercase letter. 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 = toupper (s); 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.