1.問題:想知道為何無法顯示16進位?
2.程式碼:
# include <stdio.h>
# include <stdlib.h>
int main(void)
{
int num;
scanf("%d", &num);
int r2, r8, r16; //餘數
int q; //商
int k; //用於"和r代換"
int i; //計數器
int degree = 0; //記錄次方數
int two = 1; //2的degree
int eight = 1; //8的degree
int sixteen = 1; //16的degree
r2 = num;
r8 = num;
r16 = num;
if(num >= 0)
{
printf("\n2進位:"); //處理2進位
while(two <= r2)
{
two = two * 2;
degree = degree + 1;
}
two = two / 2;
degree = degree - 1;
for(i = degree; i > -1; i = i - 1)
{
if(two <= r2)
{
k = r2 % two;
q = (r2 - k) / two;
printf("%d", q);
r2 = k;
two = two / 2;
}
else
{
printf("0");
two = two / 2;
}
}
printf("\n"); //交界
printf("\n8進位:"); //處理8進位
while(eight <= r8)
{
eight = eight * 8;
degree = degree + 1;
}
eight = eight / 8;
degree = degree - 1;
for(i = degree; i > -1; i = i - 1)
{
if(eight <= r8)
{
k = r8 % eight;
q = (r8 - k) / eight;
printf("%d", q);
r8 = k;
eight = eight / 8;
}
else
{
printf("0");
eight = eight / 8;
}
}
printf("\n"); //交界
printf("\n16進位:"); //處理16進位
while(sixteen <= r16)
{
sixteen = sixteen * 16;
degree = degree + 1;
}
sixteen = sixteen / 16;
degree = degree - 1;
for(i = degree; i > -1; i = i - 1)
{
if(sixteen <= r16)
{
k = r16 % sixteen;
q = (r16 - k) / sixteen;
if(q >= 1 && q <= 9)
printf("%d", q);
else
{
if(q == 10)
printf("A");
else if(q == 11)
printf("B");
else if(q == 12)
printf("C");
else if(q == 13)
printf("D");
else if(q == 14)
printf("E");
else if(q == 15)
printf("F");
}
r16 = k;
sixteen = sixteen / 16;
}
else
{
printf("0");
sixteen = sixteen / 16;
}
}
}
system("pause");
return 0;
}
3.執行畫面
degree 在算完一個進位之後要「歸零」
加了兩列degree = 0;
後如下
# include <stdio.h>
# include <stdlib.h>
int main(void)
{
int num;
scanf("%d", &num);
int r2, r8, r16; //餘數
int q; //商
int k; //用於"和r代換"
int i; //計數器
int degree = 0; //記錄次方數
int two = 1; //2的degree
int eight = 1; //8的degree
int sixteen = 1; //16的degree
r2 = num;
r8 = num;
r16 = num;
if(num >= 0)
{
printf("\n2進位:"); //處理2進位
while(two <= r2)
{
two = two * 2;
degree = degree + 1;
}
two = two / 2;
degree = degree - 1;
for(i = degree; i > -1; i = i - 1)
{
if(two <= r2)
{
k = r2 % two;
q = (r2 - k) / two;
printf("%d", q);
r2 = k;
two = two / 2;
}
else
{
printf("0");
two = two / 2;
}
}
printf("\n"); //交界
degree = 0;
printf("\n8進位:"); //處理8進位
while(eight <= r8)
{
eight = eight * 8;
degree = degree + 1;
}
eight = eight / 8;
degree = degree - 1;
for(i = degree; i > -1; i = i - 1)
{
if(eight <= r8)
{
k = r8 % eight;
q = (r8 - k) / eight;
printf("%d", q);
r8 = k;
eight = eight / 8;
}
else
{
printf("0");
eight = eight / 8;
}
}
printf("\n"); //交界
degree = 0;
printf("\n16進位:"); //處理16進位
while(sixteen <= r16)
{
sixteen = sixteen * 16;
degree = degree + 1;
}
sixteen = sixteen / 16;
degree = degree - 1;
for(i = degree; i > -1; i = i - 1)
{
if(sixteen <= r16)
{
k = r16 % sixteen;
q = (r16 - k) / sixteen;
if(q >= 1 && q <= 9)
printf("%d", q);
else
{
if(q == 10)
printf("A");
else if(q == 11)
printf("B");
else if(q == 12)
printf("C");
else if(q == 13)
printf("D");
else if(q == 14)
printf("E");
else if(q == 15)
printf("F");
}
r16 = k;
sixteen = sixteen / 16;
}
else
{
printf("0");
sixteen = sixteen / 16;
}
}
}
return 0;
}
輸出十六進位字串的方法不是很簡單嗎?如果不是學校作業需要,我還真不會想去自己刻鋼板
printf("%04X", 4779); // 輸出 12AB
加一個二進位的另類方法
const char *bit_rep[16] = {
[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
};
void print_byte(uint8_t byte)
{
printf("%s%s", bit_rep[byte >> 4], bit_rep[byte & 0x0F]);
}