iT邦幫忙

0

16進位問題

  • 分享至 

  • xImage

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.執行畫面
https://ithelp.ithome.com.tw/upload/images/20201011/20128427JLDNFMJzxp.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-10-11 14:02:51
最佳解答

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;
	
}

另外,點這裡是我這次鐵人賽唯一的一篇文章,喜歡的話左上角點 Like

憤怒鳥 iT邦新手 5 級 ‧ 2020-10-11 19:34:42 檢舉

我懂了,因為每次都要使degree = 0,再去計算次方,就想一開始計算2進位一樣

憤怒鳥 iT邦新手 5 級 ‧ 2020-10-11 19:35:04 檢舉

謝謝你.

我懂了,因為每次都要使degree = 0,再去計算次方,就想一開始計算2進位一樣

有進步就是好事
給你按個
/images/emoticon/emoticon12.gif

1
japhenchen
iT邦超人 1 級 ‧ 2020-10-12 08:26:25

輸出十六進位字串的方法不是很簡單嗎?如果不是學校作業需要,我還真不會想去自己刻鋼板

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]);
}

我要發表回答

立即登入回答