C language string and digital conversion functions

Source: Internet
Author: User
Tags define function float number integer numbers

Atof (convert a string to a float number) atoi (convert a string to an integer number) atol (convert a string to an integer number)

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)

1. atof (converts a string to a float number)
Header file: # include <stdlib. h>
Define the function: Double atof (const char * nptr );
Function Description: atof () 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.
('/0') to end the conversion and return the result. 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 */<br/> # include <stdlib. h> <br/> void main () <br/>{< br/> char * A = "-100.23"; <br/> char * B = "200e-2 "; <br/> float C; <br/> C = atof (A) + atof (B); <br/> printf ("c = %. 2f/N ", c); <br/>}< br/>

 

Running result c =-98.23

 

2. atoi (convert a string to an integer)
Header file: # include <stdlib. h>
Define the function: int atoi (const char * nptr );
Function Description: atoi () 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.
('/0') to end the conversion and return the result.
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 */<br/> # include <stdlib. h> <br/> void Mian () <br/>{< br/> char a [] = "-100 "; <br/> char B [] = "456"; <br/> int C; <br/> C = atoi (A) + atoi (B ); <br/> printf ("c = % d/N", c); <br/>}< br/>

Run Result c = 356

3. atol (converts strings into integer numbers)
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 until the conversion starts when a number or positive or negative sign is encountered, and then ends when a non-digit or string ends.
('/0') to end the conversion and return the result.
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 */<br/> # include <stdlib. h> <br/> void main () <br/>{< br/> char a [] = "1000000000 "; <br/> char B [] = "234567890"; <br/> long C; <br/> C = atol (A) + atol (B ); <br/> printf ("c = % d/N", c); <br/>}< br/>

Run Result c = 1234567890

4. gcvt (convert the float number to a string and rounding it out)
Header file: # include <stdlib. h>
Function Definition: char * gcvt (double number, size_t ndigits, char * BUF );
Function Description: gcvt () is used to convert the parameter number into an ASCII string. The parameter ndigits indicates the number of digits displayed. Gcvt () is different from ECVT () and fcvt () in

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> <br/> void main () <br/>{< br/> double A = 123.45; <br/> double B =-1234.56; <br/> char * PTR; <br/> int decpt, sign; <br/> gcvt (A, 5, PTR ); <br/> printf ("A value = % s/n", PTR); <br/> PTR = gcvt (B, 6, PTR ); <br/> printf ("B value = % s/n", PTR); <br/>} 
Running result a value = 123.45
B value =-1234.56

5. strtodd (convert a string to a floating point number)
Header file: # include <stdlib. h>
Define the function: Double strtodd (const char * nptr, char ** endptr );
Function Description: strtodd () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered. When a non-digit or string ends
('/0') to end the conversion and return the result. 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 (10, 2, and 16) */<br/> # include <stdlib. h> <br/> void Mian () <br/>{< br/> char a [] = "1000000000 "; <br/> char B [] = "1000000000"; <br/> char C [] = "ffff "; <br/> printf ("A = % d/N", strtodd (A, null, 10); <br/> printf ("B = % d/N ", strtodd (B, null, 2); <br/> printf ("c = % d/N", strtodd (C, null, 16 )); <br/>}< br/>

Running result a = 1000000000
B = 512
C = 65535.

6. strtol (convert the string into a growth integer)
Header file: # include <stdlib. h>
Define the function: Long int strtol (const char * nptr, char ** endptr, int base );
Function Description: strtol () converts the nptr parameter string 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 operator used.

If the base value is 10, the base value is in hexadecimal format. If the base value is 16, the base value is in hexadecimal format. When the base value is 0, 10 is used for conversion, but before '0x ',

If this parameter is set to a character, the hexadecimal conversion is performed. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it is opened in case of a number or a positive or negative sign.

Start conversion, and end the conversion when a non-number or string ends ('/0'), and return the result. If the endptr parameter is not null, it will end with an exception

The character pointer in nptr is returned by 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:

/* Convert the string A, B, and C into numbers in hexadecimal notation (10, 2, and 16) */<br/> # include <stdlib. h> <br/> void main () <br/>{< br/> char a [] = "1000000000 "; <br/> char B [] = "1000000000"; <br/> char C [] = "ffff "; <br/> printf ("A = % d/N", strtol (A, null, 10); <br/> printf ("B = % d/N ", strtol (B, null, 2); <br/> printf ("c = % d/N", strtol (C, null, 16 )); <br/>}< br/>

Running result a = 1000000000
B = 512
C = 65535.

 

7. strtoul (convert a string to an unsigned long integer)
Header file: # include <stdlib. h>
Define the function: Unsigned long int strtoul (const char * nptr, char ** endptr, int base );

Function Description: strtoul () converts the nptr parameter string to an unsigned long integer value based on the base parameter. The base parameter ranges from 2 to 36, or 0. Parameter base

In hexadecimal mode. For example, if the base value is 10, the base value is in hexadecimal mode. If the base value is 16, the base value is in hexadecimal mode. When the base value is 0, 10 is used for conversion,

However, if the '0x 'prefix character is encountered, the hexadecimal conversion will be made. At first, strtoul () scans the nptr parameter string and skips the leading space string

The conversion starts only when the character or positive or negative sign is used. When a non-digit 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 terminates unexpectedly 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 ()

8. toascii (convert an integer to a valid ASCII character)
Header file: # include <ctype. h>
Define 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 to an ASCII character.
Return Value: returns the converted ASCII code.
Example:

# Include <stdlib. h> <br/> void main () <br/>{< br/> int A = 217; <br/> char B; <br/> printf ("before toascii (): A value = % d (% C)/n", a, ); <br/> B = toascii (a); <br/> printf ("after toascii (): A value = % d (% C)/n", B, b); <br/>} 
Running result before toascii (): A value = 217 ()
After toascii (): A value = 89 (y)

9. tolower (converts uppercase letters to lowercase letters)
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 */<br/> # include <ctype. h> <br/> void main () <br/>{< br/> char s [] = "abcdefgh12345 ;! # $ "; <Br/> int I; <br/> printf (" before tolower (): % s/n ", S ); <br/> for (I = 0; I <sizeof (s); I ++) </P> <p >{< br/> S [I] = tolower (s [I]); </P> <p >}< br/> printf ("after tolower (): % s/n", S ); <br/>}</P> <p>

Running result before tolower (): abcdefgh12345 ;! # $
After tolower (): abcdefgh12345 ;! # $

10. toupper (converts lowercase letters to uppercase letters)
Header file: # include <ctype. h>
Define 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 */<br/> # include <ctype. h> <br/> void main () <br/>{< br/> char s [] = "abcdefgh12345 ;! # $ "; <Br/> int I; <br/> printf (" before toupper (): % s/n ", S ); <br/> for (I = 0; I <sizeof (s); I ++) </P> <p >{< br/> S [I] = toupper (s [I]); </P> <p >}< br/> printf ("after toupper (): % s/n", S ); <br/>}</P> <p>

Running result 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.