函数名 | 函数原型 | 头文件 | 函数功能 | 返回值 | 附加说明 |
atof | double atof(const char *nptr) | #include <stdlib.h> | 将字符串转换成浮点型数 | 返回转换后的浮点数 | 与使用strtod(nptr,(char**)NULL)结果相同 |
atoi | int atoi(const char *nptr) | #include <stdlib.h> | 将字符串转换成整形数 | 返回转换后的整型数 | 与使用strtol(nptr,(char**)NULL,10)结果相同 |
atol | long atol(const char **nptr) | #include <stdlib.h> | 将字符串转换成长整型 atol会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始转换,而在遇到非数字或字符串结束时(‘\0‘)才结束转换,并将结果返回 |
返回转换后的长整型数 | 与使用strtol(nptr,(char**)NULL,10)结果相同 |
ecvt | char *ecvt(double number, int ndigits, int *decpt, int *sign) | #include <stdlib.h> | 将浮点型数据转换成字符串,取四舍五入。 Ecvt用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。若转换成功,参数decpt指针所指的变量会返回数值中小数点的地址(从左至右算起),而参数singn指针所指的变量则代表数值正或负,若数值为正,则返回值则为0,否则为1. |
返回一个字符串指针,此字符串生命为static,若再调用ecvt() 或 fcvt() 此字符串内容会被覆盖 | 尽量改用sprintf() 做转换 |
fcvt | char *fcvt(double number, int ndigits,int *decpt,int *sign); | #include <stdlib.h> | 将浮点型数转换成字符串,取四舍五入。 fcvt用来将参数number转换成ASCII码字符串,参数ndigits表示小数点后显示的位数。若转换成功,参数decpt指针所指的变量会返回数值中小数点的地址(从左至右算起),而参数singn指针所指的变量则代表数值正或负,若数值为正,则返回值则为0,否则为1. |
返回一个字符串指针,此字符串生命为static,若再调用ecvt() 或 fcvt() 此字符串内容会被覆盖 | 尽量改用sprintf() 做转换 |
gcvt | char *gcvt(double number, size_t ndigits, char *buf); | #include <stdlib.h> | 将浮点型数转换为字符串,取四舍五入。 Gcvt用来将number转换成ASCII码字符串,参数ndigits表示显示的位数。Gcvt与ecvt 和 fcvt不同的地方在于,gcvt所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。 |
返回一字符串指针,此地址即为buf指针。 | |
strtod | double strtod(const char *nptr, char **endptr); | #include <stdlib.h> | 将字符串转换成浮点型数。 Strtod会扫描参数nptr,跳过前面的空格字符,直到遇上数字或者正负符号才开始做转换,直到出现非数字或者字符串结束符(‘\0‘)才结束转换。并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符可包含正负号、小数点或者E( e )来表示指数部分。 |
返回转换后的浮点型数 | |
strtol | long int strtol(const char * nptr, char **endptr, int base); | #include <stdlib.h> | strtol会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或者0。 参数base代表采用的进制方式,如base值为10 则采用10进制,若base值为16,则采用16进制等。当base为0时,则采用10进制转换,但遇到如‘0x‘前置字符则会使用16进制做转换。一开始strtol会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,在遇到非数字或者字符串结束符(‘\0‘)结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。 | 返回转换后的长整型数,否则返回ERANGE并将错误代码存入ermo中。 | ERANGE制定的转换字符超过合法范围 |
strtoul | unsigned long int strtoul(const char *nptr,char **endptr,int base) | #include <stdlib.h> | 将字符串转换成无符号长整型数 | ||
toascii | int toascii(int c); | #include <ctype.h> | 将整形数转换成合法的ASCII码字符。 Toascii会将参数c转换成7位的unsigned char值,第八位则会被清除,此字符机会被转成ASCII码字符。 |
将转换成功的ASCII字符值返回 | |
tolower | int tolower(int c) | #include <ctype.h> | 将大写字母转换成小写字母 | 返回转换后的小写字母。 | |
toupper | int toupper(int c) | #include <ctype.h> | 将小写字母转换成大写字母 | 返回转换后的大写字母。 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 |
#include <stdlib.h> #include <stdio.h> void
main() { //atof char
*a= "-100.23" ; char
*b= " 200e-2" ; float
c; c = atof (a) + atof (b); printf ( "c=%.2f\n" , c); printf ( "atoi(-100.23) = %d\n" , atoi (a)); printf ( "atoi( 200e-2) = %d\n" , atoi (b)); printf ( "atoi(-100.23) + atoi( 200e-2) = %d\n" , atoi (a)+ atoi (b)); //atoi char
a1[]= "-100" ; char
b1[]= " 456" ; int
c1; c1 = atoi (a1) + atoi (b1); printf ( "c1 = %d\n"
,c1 ); //atol char
a2[]= "1000000000" ; char
b2[]= " 234567890" ; long
c2; c2 = atol (a2) + atol (b2); printf ( "c2=%ld\n" ,c2); //ecvt double
a3=123.45; double
b3=-1234.56; char
*ptr; int
decpt,sign; ptr = ecvt (a3, 5, &decpt, &sign); printf ( "decept = %d, sign = %d, a value=%s\n" ,decpt,sign,ptr); ptr = ecvt(b3,6,&decpt, & sign); printf ( "decept = %d, sign = %d, b value=%s\n" ,decpt,sign,ptr); //fcvt double
a4=123.45; double
b4=-1234.567; char
*ptr4; int
decpt4, sign4; ptr4 = fcvt (a4, 2, &decpt4, &sign4); printf ( "decept = %d, sign=%d, a value=%s\n" ,decpt4,sign4,ptr4); ptr4=fcvt(b4,2,&decpt4, &sign4); printf ( "decept=%d,sign=%d,b value=%s\n" ,decpt4,sign4,ptr4); //gcvt double
a5=123.45; double
b5=-1234.56; char
ptr5[25]; int
decpt5, sign5; gcvt(a5, 5, ptr5); printf ( "a value=%s\n" ,ptr5); gcvt(b5,5,ptr5); printf ( "b value=%s\n" ,ptr5); //strtod char
*endptr6; char
a6[]= "12345.6789" ; char
b6[]= "123456qwer" ; char
c6[]= "-232.23e4" ; printf ( "a=%lf\n" , strtod (a6, NULL) ); printf ( "b=%lf\n" , strtod (b6,&endptr6)); printf ( "endptr=%s\n" , endptr6); printf ( "c=%lf\n" , strtod (c6, NULL)); //strtol char
a7[]= "1000000000" ; char
b7[]= "1000" ; char
c7[]= " fff" ; printf
( "a=%d\n" , strtol (a7,NULL,0)); printf
( "b=%d\n" , strtol (b7,NULL,2)); printf
( "c=%d\n" , strtol (c7,NULL,16)); } |
linux C函数练习 一 数据转换函数,布布扣,bubuko.com
原文:http://www.cnblogs.com/Eastsong/p/3593695.html