编一函数,将一个十进制整数转换为十六进制整数

用c语言编写
2025-01-01 02:13:47
推荐回答(2个)
回答1:

这段代码是我用C语言写的,可以实现多个功能的进制转换,你参考参考:
#include
#include
#include

void menu();
int BinToBex(char str[100],int b);
char * BexToBTH(int n,int b);

int main()
{
int x;

menu();
while (1)
{
do
{
printf(" ☆★输入1-12的序号,选择你要转换的方式,输入0退出★☆\n您输入的是:");
scanf("%d",&x);
if(x>12||x<0)
printf("请看清楚序号是1-12哦→在给你一次机会\n");
}while(x>12||x<0);

char str[100];
int n;

switch(x)
{
case 1:
printf("Please input a binary number:");
scanf("%s",str);
n=BinToBex(str,2);
printf("转换后的数字为:%s\n",BexToBTH(n,8));
break;
case 2:
printf("Please input a binary number:");
scanf("%s",str);
printf("转换后的数字为:%d\n",BinToBex(str,2));
break;
case 3:
printf("Please input a binary number:");
scanf("%s",str);
n=BinToBex(str,2);
printf("转换后的数字为:%s\n",BexToBTH(n,16));
break;
case 4:
printf("Please input a number:");
scanf("%d",&n);
printf("转换后的数字为:%s\n",BexToBTH(n,2));
break;
case 5:
printf("Please input a number:");
scanf("%d",&n);
printf("转换后的数字为:%s\n",BexToBTH(n,8));
break;
case 6:
printf("Please input a number:");
scanf("%d",&n);
printf("转换后的数字为:%s\n",BexToBTH(n,16));
break;
case 7:
printf("Please input a binary number:");
scanf("转换后的数字为:%s",str);
n=BinToBex(str,8);
printf("转换后的数字为:%s\n",BexToBTH(n,2));
break;
case 8:
printf("Please input a binary number:");
scanf("%s",str);
printf("转换后的数字为:%d\n",BinToBex(str,8));
break;
case 9:
printf("Please input a binary number:");
scanf("%s",str);
n=BinToBex(str,8);
printf("转换后的数字为:%s\n",BexToBTH(n,16));
break;
case 10:
printf("Please input a binary number:");
scanf("%s",str);
n=BinToBex(str,16);
printf("转换后的数字为:%s\n",BexToBTH(n,2));
break;
case 11:
printf("Please input a binary number:");
scanf("%s",str);
n=BinToBex(str,16);
printf("转换后的数字为:%s\n",BexToBTH(n,8));
break;
case 12:
printf("Please input a binary number:");
scanf("%s",str);
printf("转换后的数字为:%d\n",BinToBex(str,16));
break;
case 0: return 1;
}

}
return 1;
}

void menu()
{
printf ("============================Welcome you!=======================================");
printf(" 1:二进制转换为八进制 ★ 4:十进制转换为二进制\n");
printf(" 2:二进制转换为十进制 ☆☆ 5:十进制转换为八进制\n");
printf(" 3:二进制转换为十六进制 ★☆★ 6:十进制转换为十六进制\n");
printf("================================================================================");
printf(" 7:八进制转换为二进制 ★☆★ 10:十六进制转换为二进制\n");
printf(" 8:八进制转换为十进制 ☆☆ 11:十六进制转换为八进制\n");
printf(" 9:八进制转换为十六进制 ★ 12:十六进制转换为十进制\n");
printf ("=======================press 0:exit the program!================================");
}

int BinToBex(char str[100],int b)
{
int i,sum=0,lenstr;

lenstr=strlen(str);

for(i=0;i{
sum+=(str[i]-'0')*(int)(pow(b,lenstr-1-i));
}
return sum;
}

char * BexToBTH(int n,int b)
{
int i,len;
char str[100];

for(i=0;n!=0;i++)
{
str[i]=n%b+48;
if (str[i]>'9') str[i]=str[i]-'9'-1+'a';
n/=b;
}
str[i]=0;
len=strlen(str);

for(i=0;i{
str[i]^=str[len-1]^=str[i]^=str[len-1];
}

return str;
}

回答2:

char buffer [33]; //用于存放转换好的十六进制字符串,可根据需要定义长度

char * inttohex(int aa)

{

static int i = 0;

if (aa < 16) //递归结束条件

{

if (aa < 10) //当前数转换成字符放入字符串

buffer[i] = aa + '0';

else

buffer[i] = aa - 10 + 'A';

buffer[i+1] = '\0'; //字符串结束标志

}

else

{

inttohex(aa / 16); //递归调用

i++; //字符串索引+1

aa %= 16; //计算当前值

if (aa < 10) //当前数转换成字符放入字符串

buffer[i] = aa + '0';

else

buffer[i] = aa - 10 + 'A';

}

return (buffer);

}