3如何把十进制转换成十六进制

如题所述

采用短除法 用要转化的十进制说除以16,得到商r1和余数a1,然后在用商r1除以16,得到商r2和余数a2,再用商r2除以16,得到.....如此进行下去 直到商位0 然后把所得的余数按从下往上的顺序排列 即得转化后的16进制数 如(1000)=(3E8)16 转化过程如下:
1000/16=62.....8
62/16=3.........14
3/16=0.........3
14即为16进制中的E 从下往上排余数既得3E8 即为所求
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-16
void main(){
char c[100];
int i = 0;
int temp;
char tempc[16];
int l;
tempc[0] = '0';
tempc[1] = '1';
tempc[2] = '2';
tempc[3] = '3';
tempc[4] = '4';
tempc[5] = '5';
tempc[6] = '6';
tempc[7] = '7';
tempc[8] = '8';
tempc[9] = '9';
tempc[10] = 'A';
tempc[11] = 'B';
tempc[12] = 'C';
tempc[13] = 'D';
tempc[14] = 'E';
tempc[15] = 'F';
scanf("%d",&l);
while(l > 0){
temp = (int)l % 16;//强制转换为int类型
c[i] = tempc[temp];//把余数转换成字符
l /= 16;
i++;
}
i--;
while(i >= 0){
printf("%c",c[i]);
i--;
}
printf("%\n");
}

满意就采为最佳答案吧^_^本回答被提问者采纳