#include <stdio.h>
#include <string.h>
#define N 20
char *hex(char *s,int num,int n)
{
char tmp;
int i = 0;
while (num != 0)
{
s[i] = num % n;
if(s[i] <= 9)
{
s[i]=s[i]+'0';
}
else
{
switch(s[i])
{
case 10:s[i]='A';
break;
case 11:s[i]='B';
break;
case 12:s[i]='C';
break;
case 13:s[i]='D';
break;
case 14:s[i]='E';
break;
case 15:s[i]='F';
break;
default:break;
}
}
num = num / n;
i++;
}
s[i]='\0';
for(i=0;i<strlen(s)/2;i++)
{
tmp = s[i];
s[i] = s[strlen(s)-i-1];
s[strlen(s)-i-1] = tmp;
}
return s;
}
int main()
{
int num;
int n;
char s[N];
printf("请输入一个整数和进制(中间以空格间隔):\n");
scanf("%d %d",&num,&n);
printf("转换后的字符串为:\n%s\n",hex(s,num,n));
return 0;
}
请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
最新推荐文章于 2022-12-20 17:10:54 发布