18, and numeric string conversion function

1, atof (string into the floating-point)  

Correlation function atoi, atol, strtod, strtol, strtoul

Header files #include <stdlib.h>

Defined functions doubleatof (const char * nptr);

Function Description the atof () scans the parameter string nptr, skip over space characters, numbers, or until the event of the sign began to do the conversion, and then at the end face or non-numeric string ( '\ 0') before the conversion is complete, and return the results. Nptr string parameter may comprise sign, decimal point, or E (e) to represent the exponent portion, 123.456 or 123e-2.

Return Value Returns the number of the floating-point conversion.

Annotated the atof () with the same result using strtod (nptr, (char **) NULL).

After the sample / * Converts a string to a character string into digital sum b * /

#include<stdlib.h>

main()

{

char *a=”-100.23”;

char*b=”200e-2”;

float c;

c=atof(a)+atof(b);

printf(“c=%.2f\n”,c);

}

2, atoi (the string into integer)  

Correlation function atof, atol, atrtod, strtol, strtoul

Header files #include <stdlib.h>

Defined functions intatoi (const char * nptr);

Function Description atoi () scans the parameter string nptr, skip over space characters, numbers, or until the event of the sign began to do the conversion, and then at the end face or non-numeric string ( '\ 0') before the conversion is complete, and return the results.

Returns the integer return value conversion.

Additional Description atoi () using strtol (nptr, (char **) NULL, 10); the same result.

After the sample / * Converts a string to a character string into digital sum b * /

#include<stdlib.h>

mian ()

{

chara[]=”-100”;

char b[]=”456”;

int c;

c=atoi(a)+atoi(b);

printf(c=%d\n”,c);

}

3, atol (convert a string grow integer)  

Correlation function atof, atoi, strtod, strtol, strtoul

Header files #include <stdlib.h>

Defined functions longatol (const char * nptr);

Function Description atol () scans the parameter string nptr, skip over space characters, numbers, or until the event of the sign began to do the conversion, and then at the end face or non-numeric string ( '\ 0') before the conversion is complete, and return the results.

Returns the number of return long integer conversion value.

Additional Description atol () using strtol (nptr, (char **) NULL, 10); the same result.

After the sample / * Converts a string to a character string into digital sum b * /

#include<stdlib.h>

main()

{

chara[]=”1000000000”;

Charb [] = "234567890";

long c;

c=atol(a)+atol(b);

printf(“c=%d\n”,c);

}

3, gcvt (converted floating-point number to a string, to take rounded)  

Correlation function ecvt, fcvt, sprintf

Header files #include <stdlib.h>

Defined function char * gcvt (double number, size_t ndigits, char * buf);

Description gcvt function () is used to convert the parameter to a number of ASCII characters, the number of bits parameter ndigits display, i.e. the length. gcvt () and the ECVT () and fcvt () difference is that, gcvt the translated string () contains a decimal point or the sign. If the conversion is successful, the converted string will be placed on the spatial parameters buf pointer.

Return Value Returns a pointer to the string, this address is a pointer buf.

Additional information  

Examples #include <stdlib.h>

main()

{

doublea=123.45;

doubleb=-1234.56;

char * ptr;

intdecpt,sign;

gcvt(a,5,ptr);

printf(“avalue=%s\n”,ptr);

ptr=gcvt(b,6,ptr);

printf(“bvalue=%s\n”,ptr);

}

4, strtod (string converted to floating point)  

Correlation function atoi, atol, strtod, strtol, strtoul

Header files #include <stdlib.h>

Defined functions doublestrtod (const char * nptr, char ** endptr);

Function Description strtod () scans the parameter string nptr, skip over space characters, numbers, or until the event of the sign began to do the conversion, when ( '\ 0') ends or non-numeric string that appears before the end of the conversion and returns the results. If endptr character pointer is not NULL, it will terminate the encounter substandard condition of the nptr returned by the endptr. Nptr string parameter may comprise sign, decimal point, or E (e) to represent the exponent portion. 123.456 or 123e-2.

Return Value Returns the number of the floating-point conversion.

Annotated Reference atof ().

Example / * string a, b, c respectively converted into digital binary 10,2,16 * /

#include<stdlib.h>

mian ()

{

chara[]=”1000000000”;

char b[]=”1000000000”;

char c[]=”ffff”;

printf(“a=%d\n”,strtod(a,NULL,10));

printf(“b=%d\n”,strtod(b,NULL,2));

printf(“c=%d\n”,strtod(c,NULL,16));

}

5, strtol (convert a string grow integer)  

Correlation function atof, atoi, atol, strtod, strtoul

Header files #include <stdlib.h>

Defined function long int strtol (const char * nptr, char ** endptr, int base);

Function Description strtol () to convert the string nptr parameter will grow according integer parameter base. Parameter base ranges from 2 to 36, or 0. Hex mode parameters representative of base employed, such as the base 10 decimal value of 10 is used, if the base value of 16 in hexadecimal like. When the base value of 0 is employed to do decimal conversion, but encountered such as the '0x' leading character hexadecimal will do the conversion. Strtol start () scans the parameter string nptr, skip over space characters, numbers, or until the event of the sign began to do the conversion, and then at the end face or non-numeric string ( '\ 0') the end of conversion and returns the results. If endptr character pointer parameter is not NULL, it will terminate the encounter substandard conditions in the nptr returned by endptr.

The return value is returned after a long integer conversion, otherwise an error code is returned and ERANGE is stored in errno.

Annotated ERANGE specified string conversion beyond the legal range.

Example / * string a, b, c respectively converted into digital binary 10,2,16 * /

#include<stdlib.h>

main()

{

chara[]=”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));

}

6, strtoul (the string into an unsigned long integer)  

Correlation function atof, atoi, atol, strtod, strtol

Header files #include <stdlib.h>

Defined function unsignedlong int strtoul (const char * nptr, char ** endptr, int base);

Function Description strtoul () will be converted into a parameter string nptr unsigned long integer parameter according base. Parameter base ranges from 2 to 36, or 0. Hex mode parameters representative of base employed, such as the base 10 decimal value of 10 is used, if the base value of 16 in hexadecimal numbers and the like. When the base value of 0 is employed to do decimal conversion, but encountered such as the '0x' leading character hexadecimal will do the conversion. Strtoul start () scans the parameter string nptr, skip the preceding space strings, numbers, or until the sign encountered began to do the conversion, and then at the end face or non-numeric string ( '\ 0') end conversion, and return the results. If endptr character pointer parameter is not NULL, it will terminate the encounter substandard conditions in the nptr returned by endptr.

The return value is returned after a long integer conversion, otherwise an error code is returned and ERANGE is stored in errno.

Annotated ERANGE specified string conversion beyond the legal range.

7, toascii (convert integer into a legal ASCII characters)  

Correlation function isascii, toupper, tolower

Header files #include <ctype.h>

Defined functions inttoascii (int c)

Function Description ToASCII () parameter c will be converted into 7-bit unsigned char value, the eighth bit will be cleared, i.e., the character is converted into ASCII characters.

The return value to convert ASCII characters successful return value.

Examples #include <stdlib.h>

main()

{

inta = 217;

char b;

printf(“beforetoascii () : a value =%d(%c)\n”,a,a);

b = toascii (a);

printf(“aftertoascii() : a value =%d(%c)\n”,b,b);

}

8, tolower (convert uppercase to lowercase)  

Correlation function isalpha, toupper

Header files #include <stdlib.h>

Defined functions inttolower (int c);

Function Description If the parameter c is then the corresponding uppercase letter lowercase return.

Return Value Returns the lowercase conversion, if the value of parameter c to be converted will be returned.

9, toupper (the lowercase letters to uppercase letters)  

Correlation function isalpha, tolower

Header files #include <ctype.h>

Defined functions inttoupper (int c);

Function Description If the parameter c is the lowercase letters uppercase letters enantiomers return.

Return Value Returns uppercase after conversion, if the value of parameter c to be converted will be returned.

 

Published 288 original articles · won praise 31 · views 10000 +

Guess you like

Origin blog.csdn.net/hopegrace/article/details/104569741