以下是代码实现:
void dec16hex(int num,char *hex)
{
char hextable[] = “0123456789ABCDEF”;
int i = 0;
int j,k;
char temhex[16],ch;
while(num)
{
hex[i++] = hextable[num%16]; //对十进制数求余并最终与hextable数组中的字符匹配
num = num/16;
}
hex[i] = ‘\0′; //给字符串从末尾加’\0’
for(j = 0,k = i-1;j < i/2;j++,k–) //将字符串反序
{
ch = hex[j];
hex[j] = hex[k];
hex[k] = ch;
}
}
int main(int argc, const char *argv[])
{
int num,ret;
char hex[16];
while(1)
{
printf(“十进制(0~256):”);
scanf(“%d”,&num);
if(num < 0)
break;
dec16hex(num,hex);
printf(“十六进制:”);
puts(hex);
}
return 0;
}
通过修改,可以实现其他进制的转换。。。。