这段代码是我用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;
}
楼上的太复杂了吧!c的话直接这样:
main()
{ int a,i;char x[16];
float b;
scanf("%d",&a);
printf("八进制为%o,十六进制为%x",a,a);
for(i=0;i<=15;i++)
{b=a/2.0;
a=a/2;
if(b==(float)a) x[15-i]='0';
else x[15-i]='1';
}
printf("二进制为%s",x);
}
你是用什么语言的啊?如果没有直接转换的话一般方式如下:
对于转换成N进制的数S,先读入数字,判断它在N^A与N^(A+1)之间,令M=0,然后进行循环:M=M*10; S DIV N^A得到的数值存入M,令N为N MOD N^A; A=A-1;直到N为0