思路:1、正數 –> (該數%16) 餘數 >= 10, 餘數 < 10;
2、負數
3、零
//十進位制轉化為十六進位制 //逆序儲存在一個字串陣列中 #include<stdio.h> char* TenToSixteen(int ten){ char str[20]; int i = 0,w = 0,temp = ten,status; if(temp>= 0) status = 1; else status = 0; switch(status){ case 1: //當為正數 if(ten==0) str[w++] = `0`; else{ while(temp> 0){ int elem = temp%16; if(elem>9){ elem = elem -10 +`A`; str[w++] = elem; } else str[w++] = elem + `0`; temp = temp/16; } } break; case 0: //當為負數 temp = -temp; while(temp> 0){ int elem = temp%16; if(elem>9){ elem = elem -10 +`A`; str[w++] = elem; } else str[w++] = elem + `0`; temp = temp/16; } str[w++] = `-`; break; default :printf("error! "); } //列印十六進位制的數 for(i = w-1;i>=0;i--){ printf("%c",str[i]); } }