C language string and numeric conversion functions

Source: Internet
Author: User
Tags float number

Atof(Convert 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 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) ;}execute c =-98.23
Atoi(Convert a string to an integer) related functions include atof, atol, atrtodd, strtol, and strtoul 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 ('\ 0') 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);} execute c = 356Atol(Convert the string to the integer number) related functions atof, atoi, strtodd, strtol, strtoul 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 ('\ 0') 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);} execute c = 1234567890Gcvt(Convert the float number to a string and rounding it out.) 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 indicates that 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. 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);} execute a value = 123.45b value =-1234.56
  Strcto(Convert a string to a floating point number) related functions atoi, atol, strtodd, strtol, strtoul header file # include <stdlib. h> define the function double strtop (const char * nptr, char ** endptr). The function indicates that 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 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);} execute a = 000000000b = 512c = 65535
Strtol(Convert string to integer) related functions include atof, atoi, atol, strtodd, and strtoul header file # include <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 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. 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. 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 = 000000000b = 512c = 65535





Strtoul(Convert a string to an unsigned long integer number) related functions atof, atoi, atol, strtodd, strtol header file # include <stdlib. h> define the 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 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 more information, see strtol ()
Toascii(Convert integer data to valid ASCII characters) 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(Convert uppercase letters to lowercase letters) related functions: 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 [I] = tolower (s [I]); printf ("after tolower (): % s \ n", s);} execute before tolower (): aBcDeFgH12345 ;! # $ After tolower (): abcdefgh12345 ;! # $Toupper(Converts lowercase letters to uppercase letters) the related functions are isalpha and the tolower header file # 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. Additional instructions/* convert lowercase letters in the s string into 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 );}
Run before toupper (): aBcDeFgH12345 ;! # $ After toupper (): ABCDEFGH12345 ;! # $
---------------------------------------------------------

1、itoa


Function: converts an integer to a string.

Usage: char * itoa (int value, char * string, int radix );

Description: itoa is the abbreviation of the English integer to array (converting the int integer into a string and saving the value in the array string.

Parameters:

Value: the integer to be converted.

Radix: indicates the base number, that is, the value is first converted to the base number of radix. The value range is between 2 and 36. For example, 10 indicates 10 hexadecimal and 16 indicates hexadecimal.

* String: Save the converted string.

Return Value:

Char *: indicates the generated string, which is the same as * string.

Note: The header file of this function is "stdlib. h"

2. ltoa

Function: converts a long integer to a string.

Usage: char * ltoa (long value, char * string, int radix );

Description: itoa is the abbreviation of long integer to array (converting the long integer of long int into a string and saving the value in the array string.

Parameters:

Value: the number of long integers to be converted.

Radix: indicates the base number, that is, the value is first converted to the base number of radix. The value range is between 2 and 36. For example, 10 indicates 10 hexadecimal and 16 indicates hexadecimal.

* String: Save the converted string.

Return Value:

Char *: indicates the generated string, which is the same as * string.

Note: The header file of this function is "stdlib. h"

3. ultoa

Function: converts an unsigned long integer to a string.

Usage: char * ultoa (unsigned long value, char * string, int radix );

Description: itoa is the abbreviation of unsigned long integer to array (converting unsigned long int unsigned long integer to a string and saving the value in the array string.

Parameters:

Value: the number of unsigned long integers to be converted.

Radix: indicates the base number, that is, the value is first converted to the base number of radix. The value range is between 2 and 36. For example, 10 indicates 10 hexadecimal and 16 indicates hexadecimal.

* String: Save the converted string.

Return Value:

Char *: indicates the generated string, which is the same as * string.

Note: The header file of this function is "stdlib. h"

4. gcvt

Function: converts the float number to a string and returns the rounded value.

Usage: char * gcvt (double value, int ndigit, char * buf );

Explanation: gcvt () is used to convert the parameter number into an ASCII string. The parameter ndigits 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.

Parameters:

Value: The floating point number to be converted.

Ndigit: The number of valid digits stored.

* Buf: The storage location of the result.

Return Value:

Char *: indicates the generated string, which is the same as * buf.

Note: The header file of this function is "stdlib. h"

5. ecvt

Function: Convert the double precision floating point to a string. The conversion result does not include the decimal point.

Usage: char * ecvt (double value, int ndigit, int * decpt, int * sign );

Explanation: The ecvt function converts a double-precision floating point number into a string. The value parameter is the floating point number to be converted. This function stores a maximum of ndigit numeric values as a string and adds an empty numeric character ('\ 0'). If the number in the value exceeds ndigit, the low digit is rounded. If the number is less than ndigit, the string is filled with 0.

Only digits are stored in the string. the decimal point and value are obtained from decpt and sign after the call. The decpt parameter indicates the integer given at the decimal point, which is calculated from the beginning of the string. 0 or a negative number indicates that the decimal point is on the left of the first number. The sign parameter specifies an integer that indicates the number of converted symbols. If the integer is 0, this is a positive number; otherwise, it is a negative number.

Parameters:

Value: double-precision floating point number to be converted.

Ndigit: The number of valid digits stored.

* Decpt: the decimal point of the storage.

* Sign: Symbol of the number of conversions.

Return Value:

Char *: indicates the generated string.

Note: The header file of this function is "stdlib. h"

6. fcvt

Function: specify the number of digits as the conversion precision.

Usage: char * fcvt (double value, int ndigit, int * decpt, int * sign );

Explanation: The fcvt function converts a double-precision floating point number into a string. The value parameter is the floating point number to be converted. The number of decimal places to retain the actual values involved in the conversion is determined by ndigit.

Only digits are stored in the string. the decimal point and value are obtained from decpt and sign after the call. The decpt parameter indicates the integer given at the decimal point, which is calculated from the beginning of the string. 0 or a negative number indicates that the decimal point is on the left of the first number. The sign parameter specifies an integer that indicates the number of converted symbols. If the integer is 0, this is a positive number; otherwise, it is a negative number.

Parameters:

Value: double-precision floating point number to be converted

Ndigit: number of digits after the decimal point in the converted string

* Decpt: returns the decimal point in the string.

* Sign: symbol.

Return Value:

Char *: indicates the generated string.

Note: The header file of this function is "stdlib. h"

Related Article

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.