各位看官們,大家好,從今天開始,我們講大型章回體科技小說 :C栗子,也就是C語(yǔ)言實(shí)例。閑話休提,言歸正轉(zhuǎn)。讓我們一起talk C栗子吧!
看官們,上一回中咱們說的是棧的例子,這一回咱們說的例子是:輸出十六進(jìn)制。
看官們,我想熟悉C語(yǔ)言的朋友們,對(duì)于輸出十六進(jìn)制的內(nèi)容,肯定都會(huì)使用printf函數(shù)進(jìn)行格式化輸出。不過,有時(shí)候想輸出十六進(jìn)制時(shí)就會(huì)有點(diǎn)“不識(shí)廬山真面目,只緣身在此山中”的感覺。我在前面的例子中 有一個(gè)關(guān)于進(jìn)制轉(zhuǎn)換的例子。當(dāng)時(shí)輸出十六進(jìn)制時(shí)使用分別判斷10到15,然后依據(jù)判斷結(jié)果輸出A到F?,F(xiàn)在回頭看這個(gè)例子,就是剛才說的“不識(shí)廬山真面目,只緣身在此山中”。為此,我專門把進(jìn)制轉(zhuǎn)換例子中的 代碼進(jìn)行了修改。使用printf函數(shù)中的%X輸出十六進(jìn)制的內(nèi)容??垂賯?,詳細(xì)的代碼如下,大家可以參考:
1 /* **************************
2 * For Hex output
3 * *************************/
4 #include<stdio.h>
5
6 /*convert function
7 *Parameter a is used for saving converting result
8 *Parameter iVal is a value ,which will be converted.
9 *Parameter base is a base value ,
10 */
11
12 #define SUCCESS 0
13 #define FALSE 1
14 #define BIT 32
15
16 int convert(int *a,int iVal,int base)
17 {
18 int index = 0;
19 int res = 0;
20
21
22 if(NULL == a)// check the pointer
23 {
24 printf("null pointer \n");
25 return FALSE;
26 }
27
28 if( 0 == base) //check the base, 0 can't be used for division
29 {
30 printf("bad value of base \n");
31 return FALSE;
32 }
33
34 res = iVal;
35 //the main algorithm
36 do
37 {
38 *(a+index) = res % base;
39 res /= base;
40 ++index;
41 }while(res != 0);
42
43 return SUCCESS;
44 }
45
46 int showConvertResult(int *a)
47 {
48 int index = 0;
49
50 if(NULL == a)// check the pointer
51 {
52 printf("null pointer \n");
53 return FALSE;
54 }
55
56 for(index=BIT; index>0; --index)
57 {
58 if(index %4 == 0)
59 printf(" ");
60
61 switch( *(a+index-1) ) // input hex character
62 {
63 case 10:
64 case 11:
65 case 12:
66 case 13:
67 case 14:
68 case 15:
69 printf("%X",a[index-1]);
70 break;
71 default:
72 printf("%d",a[index-1]);
73 break;
74 }
75 }
76
77 printf("\n");
78
79 return SUCCESS;
80 }
81
82 int main()
83 {
84 int base = 0;
85 int iVal =0;
86 int result = 0;
87 int hex[BIT] = {0}; // the max bit is 32
88
89 printf("please input a number for conversion: \n");
90 if(0 == scanf("%d",&iVal))
91 {
92 printf("Input number is not right \n");
93 return FALSE;
94 }
95
96 printf("please select a char for conversion: \n");
97 printf(" \t ->Decimal conver to Binary:input 1 : \n");
98 printf(" \t ->Decimal conver to Octonary:input 2 : \n");
99 printf(" \t ->Decimal conver to Hex:input 3 : \n");
100
101 if(0 == scanf("%d",&base))
102 {
103 printf("Input number for selecting is not right \n");
104 return FALSE;
105 }
106
107 switch(base)
108 {
109 case 1:
110 result = convert(hex,iVal,2);
111 break;
112
113 case 2:
114 result = convert(hex,iVal,8);
115 break;
116
117 case 3:
118 result = convert(hex,iVal,16);
119 break;
120
121 default:
122 break;
123
124 }
125
126 if(result == FALSE)
127 return FALSE;
128 else
129 {
130 showConvertResult(hex);
131 return SUCCESS;
132 }
133 }
該例子的代碼使用了前面進(jìn)制轉(zhuǎn)換的代碼,只修改了其中輸出十六進(jìn)制的內(nèi)容,因此該例子也可以看作是進(jìn)制轉(zhuǎn)換例子的改進(jìn)版。只所以寫該例子就是為了避免“不識(shí)廬山真面目,只緣身在此山中”這種感覺。
各位看官,關(guān)于輸出十六進(jìn)制的例子咱們就說到這里。欲知后面還有什么例子,且聽下回分解。