sprintf与sscanf是非常有用的格式转换函数。
下面以整型10进制与16进制的转换为例,希望对大家了解转换方法有点用
1 #include
2
3 int main(void)
4 {
5 char s1[10] = {0};
6 char s2[10] = {0};
7 char s3[10] = {0};
8 char s4[10] = {0};
9
10 int i1 = 0;
11 int i2 = 0;
12 int i3 = 0;
13 int i4 = 0;
14
15 sprintf(s1, "[%d]", 10);
16 sprintf(s2, "[%x]", 10);
17 sprintf(s3, "[%d]", 0xa);
18 sprintf(s4, "[%x]", 0xa);
19
20 puts(s1);
21 puts(s2);
22 puts(s3);
23 puts(s4);
24 puts("------------");
25
26 sscanf("[10]","[%d]", &i1);
27 sscanf("[a]","[%d]", &i2);
28 sscanf("[10]","[%x]", &i3);
29 sscanf("[a]","[%x]", &i4);
30
31 printf("i1[%d]\n", i1);
32 printf("i2[%d]\n", i2);
33 printf("i3[%d]\n", i3);
34 printf("i4[%d]\n", i4);
35
36 }
OUT:
[10]
[a]
[10]
[a]
------------
i1[10]
i2[0]
i3[16]
i4[10]
阅读(1751) | 评论(0) | 转发(0) |