用C语言编程实现10进制数值转16进制字符串

2020-06-22 科技 113阅读
#include
#define N 8
void tran(int num,int k)
{
int arr[N],i;
for (i=0;i {
arr[i]=num%k;
num=num/k;
if (num==0)
break;
}
printf("转换为%d进制数为: ",k);
for (;i>=0;i--)
switch (arr[i])
{
case 10: printf("A");break;
case 11: printf("B");break;
case 12: printf("C");break;
case 13: printf("D");break;
case 14: printf("E");break;
case 15: printf("F");break;
default: printf("%d",arr[i]);
}
printf("\n\n\n");
}
void main()
{
int num,choo;
loop: printf("请选择功能:0.退出 1.十进制转二进制 2.十进制转八进制 3.十进制转十六进制\n");
scanf("%d",&choo);
switch (choo)
{
case 1:
printf("请输入要转换的十进制数:");
scanf("%d",&num);
tran (num,2);
goto loop;
case 2:
printf("请输入要转换的十进制数:");
scanf("%d",&num);
tran (num,8);
goto loop;
case 3:
printf("请输入要转换的十进制数:");
scanf("%d",&num);
tran (num,16);
goto loop;
case 0:break;
default :
printf("输入有误!请重新输入!\n");
goto loop;
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com