思路: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!\n"); } //打印十六进制的数 for(i = w-1;i>=0;i--){ printf("%c",str[i]); } }