二进制十进制十六进制的相互转换
发布网友
发布时间:2022-05-16 11:49
我来回答
共2个回答
热心网友
时间:2023-10-21 18:30
#include "iomanip"
#include "stdlib.h"
#include "conio.h"
#include "string"
#include "math.h"
#include "iostream"
int mainmenu();
int dec_to_bin(); //10->2
int bin_to_dec(); //2->10
void tran2_16(); //2->十六进制
int tran16_2(); //16->2
int hex_dec(); //十六进制到十进制
int dec_hex(); //十进制到十六进制
#define N 20
#define Q 5
using namespace std;
string ch2_16(string ch);
char B2H4bit(string s)
{
char ret;
if (s == "0000") ret = '0';
else if (s == "0001") ret = '1';
else if (s == "0010") ret = '2';
else if (s == "0011") ret = '3';
else if (s == "0100") ret = '4';
else if (s == "0101") ret = '5';
else if (s == "0110") ret = '6';
else if (s == "0111") ret = '7';
else if (s == "1000") ret = '8';
else if (s == "1001") ret = '9';
else if (s == "1010") ret = 'A';
else if (s == "1011") ret = 'B';
else if (s == "1100") ret = 'C';
else if (s == "1101") ret = 'D';
else if (s == "1110") ret = 'E';
else if (s == "1111") ret = 'F';
else ret='\0'; // 使之输出时能于此结束,而避免出现乱码了。
return ret; //不过为什么他一定要放在main函数之前才行。??????
}
void main()
{
mainmenu();
}
int mainmenu()
{
cout<<"============进制转换器==============\n";
cout<<"----------------------请选择数字菜单\n";
cout<<" 1.十进制转换为二进制 \n";
cout<<" 2.二进制转换为十进制 \n";
cout<<" 3.二进制转换为十六进制 \n";
cout<<" 4.十六进制转换为二进制 \n";
cout<<" 5.十六进制转换为十进制 \n";
cout<<" 6.十进制转换为十六进制 \n";
cout<<" 7.退出 \n";
cout<<"====================================\n";
char ch=getch()();
switch(ch-48)
{case 1 : dec_to_bin(); break;
case 2 : bin_to_dec(); break;
case 3 : tran2_16(); break;
case 4 : tran16_2(); break;
case 5 : hex_dec(); break;
case 6 : dec_hex(); break;
case 7 : return 0;
}
}
int dec_to_bin() //十进制到二进制
{
char b[N];
char ch[N];
string all;
cout<<"请输入待转换的十进制数:";
cin>>b;
long d=atol(b);
long bb=d;
int i=0,flag=-1;
int j;
while(ch[i]!='\0')
{
if(ch[i]=='.')
{
flag=i;
break;
}
i++;
}
i=0;
while(d!=0)
{ch[i]=d%2+'0';
d=d/2;
all=ch[i]+all; //加在前面
i++;
}
if(flag==-1)
{cout<<all<<endl;
return 0;
}
all=all+".";
double x=atof(b) ;
x=x-(double)bb; //you xiao shu此时d已经为0,故不用d而用之前记下的bb
int r=0;
while(1)
{if(x*2.0>1.0)
{
all=all+"1";
x=x*2.0-1;
r++;
}
else if(x*2.0<1.0)
{
all=all+"0";
x=x*2.0;
r++;
}
else if(x*2.0==1.0)
{
all=all+"1";
break;}
if(r>=Q) //达到精度M
break;
}
cout<<"转换后所得对应二进制为:";
cout<<all<<endl;
}
int bin_to_dec() //二进制转到十进制
{
char ch[N];
float a=0; //
cout<<"请输入待转换的二进制数:";
cin>>ch; //这里应该改为ch
int j;
int i=0,flag=-1;
while(ch[i]!='\0')
{
if(ch[i]=='.')
{
flag=i;
break;
}
i++;
}
i=0;
if(flag==-1)
{while(ch[i]!='\0')
{
a=a*2+ch[i]-'0';
i++;
}
cout<<a<<endl;
cout<<"请按任意键返回\n";
getch()();
system("cls");
mainmenu();
return 0;
}
double count=0.0;
double bin=1.0/2;
i=0;
for(j=0;j<flag;j++)
a=a*2.0+ch[j]-'0'; //zheng shu bu fen
int d=strlen(ch);
j=flag+1;
for(;j<d;j++)
{
count=count+(ch[j]-'0')*bin; //b改为ch
bin/=2.0;
if(j>Q)
break;
}
cout<<"转换后所得对应二进制为:";
cout<<(a+count)<<endl;
cout<<"请按任意键返回\n";
getch()();
system("cls");
mainmenu();
return 0;
}
//2->16
void tran2_16()
{
cout<<"请输入二进制数据"<<endl; //缺点,对于整数的也要加小数点才行,尽快改进。
char ch[N];
cin>>ch;
ch2_16(ch);
}
string ch2_16(string ch)
{
string ab,cd;
int i=0;
while(ch[i])
{if(ch[i]=='.')
break;
i++;
}
int count=i;
int d=ch.length();
ab=ch.substr(0,i); //zhengshu
cd=ch.substr(i+1,d-i-1); //xiaoshu
int aa=4-i%4; //求出还要前面不补几个0
int cc=4-(d-i-1)%4; //后面补0
for(i=0;i<aa;i++)
ab="0"+ab;
for(i=0;i<cc;i++)
cd=cd+"0";
string ef;
string ret;
for(i=0;i<cc+count;i=i+4)
{ef=ab.substr(i,4);
ret+=B2H4bit(ef); //只能用+=这份符号,而不能+吗??,keyi de
}
string dot;
for(i=0;i<d-count-1;i=i+4) //此时cd为新的字符串,从0 又开始计数。
{ef=cd.substr(i,4);
dot+=B2H4bit(ef); //只能用+=这份符号,而不能+吗??,ke yi de
}
cout<<ret; //ret 此时已经结束输出了。
cout<<".";
cout<<dot<<endl;
return ret;
}
//16->2222
int tran16_2()
{
cout<<"请输入十六进制字符\n"<<endl;
char ch[N];
string ret;
cin>>ch;
int d=strlen(ch);
int i=0, flag=-1;
while(ch[i])
{if(ch[i]=='.')
{flag=i; //flag记下小数点
break;}
i++;
}
int j;
for(j=0;j<flag;j++)
{if (ch[j]=='0') ret+="0000";
else if (ch[j]=='1') ret+="0001";
else if (ch[j]=='2') ret+="0010";
else if (ch[j]=='3') ret+="0011";
else if (ch[j]=='4') ret+="0100";
else if (ch[j]=='5') ret+="0101";
else if (ch[j]=='6') ret+="0110";
else if (ch[j]=='7') ret+="0111";
else if (ch[j]=='8') ret+="1000";
else if (ch[j]=='9') ret+="1001";
else if (ch[j]=='A'||ch[j]=='a') ret+="1010";
else if (ch[j]=='B'||ch[j]=='b') ret+="1011";
else if (ch[j]=='C'||ch[j]=='c') ret+="1100";
else if (ch[j]=='D'||ch[j]=='d') ret+="1101";
else if (ch[j]=='E'||ch[j]=='e') ret+="1110";
else if (ch[j]=='F'||ch[j]=='f') ret+="1111";
} //就是这里出错,导致了没有小数点就不能处理整数部分了
if(flag!=-1)
ret=ret+".";
else
{while(ch[j]!='\0')
{if (ch[j]=='0') ret+="0000";
else if (ch[j]=='1') ret+="0001";
else if (ch[j]=='2') ret+="0010";
else if (ch[j]=='3') ret+="0011";
else if (ch[j]=='4') ret+="0100";
else if (ch[j]=='5') ret+="0101";
else if (ch[j]=='6') ret+="0110";
else if (ch[j]=='7') ret+="0111";
else if (ch[j]=='8') ret+="1000";
else if (ch[j]=='9') ret+="1001";
else if (ch[j]=='A'||ch[j]=='a') ret+="1010";
else if (ch[j]=='B'||ch[j]=='b') ret+="1011";
else if (ch[j]=='C'||ch[j]=='c') ret+="1100";
else if (ch[j]=='D'||ch[j]=='d') ret+="1101";
else if (ch[j]=='E'||ch[j]=='e') ret+="1110";
else if (ch[j]=='F'||ch[j]=='f') ret+="1111";
j++;
}
cout<<"所得二进制为:";
cout<<ret<<endl; //输出整数,结束
cout<<"请按任意键返回\n";
getch();
system("cls");
mainmenu();
return 0;
}
for(j=i;j<d;j++)
if (ch[j]=='0') ret+="0000";
else if (ch[j]=='1') ret+="0001";
else if (ch[j]=='2') ret+="0010";
else if (ch[j]=='3') ret+="0011";
else if (ch[j]=='4') ret+="0100";
else if (ch[j]=='5') ret+="0101";
else if (ch[j]=='6') ret+="0110";
else if (ch[j]=='7') ret+="0111";
else if (ch[j]=='8') ret+="1000";
else if (ch[j]=='9') ret+="1001";
else if (ch[j]=='A'||ch[j]=='a') ret+="1010";
else if (ch[j]=='B'||ch[j]=='b') ret+="1011";
else if (ch[j]=='C'||ch[j]=='c') ret+="1100";
else if (ch[j]=='D'||ch[j]=='d') ret+="1101";
else if (ch[j]=='E'||ch[j]=='e') ret+="1110";
else if (ch[j]=='F'||ch[j]=='f') ret+="1111";
cout<<"所得二进制为:";
cout<<ret<<endl;
cout<<"请按任意键返回\n";
getch();
system("cls");
mainmenu();
return 0;
}
int hex_dec()
{
//16->10
cout<<"输入十六进制";
char ch[N];
int b=0,a=0;
cin>>ch;
int i=0,flag=-1;
while(ch[i]!='\0')
{
if(ch[i]=='.')
{
flag=i;
break;
}
i++;
}
i=0;
if(flag==-1)
{while(ch[i]!='\0')
{
if(ch[i]>='a'&&ch[i]<='f')
b=ch[i]-'a'+10;
else if(ch[i]>='A'&&ch[i]<='F')
b=ch[i]-'A'+10;
else if(ch[i]>='0'&&ch[i]<='9')
b=ch[i]-'0';
a=a*16+b;
i++;
}
cout<<a<<endl;
cout<<"请按任意键返回\n";
getch();
system("cls");
mainmenu();
return 0;
}
double count=0.0;
double bin=1.0/16;
i=0;
int j;
for(i=0;i<flag;i++)
{if(ch[i]>='a'&&ch[i]<='f')
b=ch[i]-'a'+10;
else if(ch[i]>='A'&&ch[i]<='F')
b=ch[i]-'A'+10;
else if(ch[i]>='0'&&ch[i]<='9')
b=ch[i]-'0';
a=a*16+b;
}
int d=strlen(ch);
i=flag+1;
for(;i<d;i++)
{if(ch[i]>='a'&&ch[i]<='f')
b=ch[i]-'a'+10;
else if(ch[i]>='A'&&ch[i]<='F')
b=ch[i]-'A'+10;
else if(ch[i]>='0'&&ch[i]<='9')
b=ch[i]-'0';
count=count+b*bin;
bin/=16.0;
if(j>Q)
break;
}
cout<<"转换后所得对应十进制为:";
cout<<setiosflags(ios_base::fixed)<<setprecision(Q)<<(a+count)<<endl; //使用格式化输出,使其保留Q,6位小数
cout<<"请按任意键返回\n";
getch();
system("cls");
mainmenu();
return 0;
}
int dec_hex() //十进制到十六进制
{
char b[N];
char ch[N];
string all;
cout<<"请输入待转换的十进制数:";
cin>>b;
long d=atol(b);
long bb=d;
int i=0,flag=-1;
int j;
while(ch[i]!='\0')
{
if(ch[i]=='.')
{
flag=i;
break;
}
i++;
}
i=0;
while(d!=0)
{ch[i]=d%16+'0';
d=d/16;
all=ch[i]+all; //加在前面
i++;
}
if(flag==-1)
{cout<<all<<endl;
cout<<"请按任意键返回\n";
getch();
system("cls");
mainmenu();
return 0;
}
all=all+".";
double x=atof(b) ;
x=x-(double)bb;
int r=0;
while(1) //乘以16,取整数
{if((int)(x*16.0)==0)
{
all=all+"0";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==1)
{
all=all+"1";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==2)
{
all=all+"2";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==3)
{
all=all+"3";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==4)
{
all=all+"4";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==5)
{
all=all+"5";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==6)
{
all=all+"6";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==7)
{
all=all+"7";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==8)
{
all=all+"8";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==9)
{
all=all+"9";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==10)
{
all=all+"a";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==11)
{
all=all+"b";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==12)
{
all=all+"c";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==13)
{
all=all+"d";
x=x*16.0-(int)(x*16.0);
r++;
}
if((int)(x*16.0)==14)
{
all=all+"e";
x=x*16.0-(int)(x*16.0);
r++;
}
else if((int)(x*16.0)==15)
{
all=all+"f";
x=x*16.0-(int)(x*16.0);
r++;
}
if(r>=Q) //达到精度M
break;
}
cout<<"转换后所得对应十六进制为:";
cout<<all<<endl;
cout<<"请按任意键返回\n";
getch();
system("cls");
mainmenu();
return 0;
}
热心网友
时间:2023-10-21 18:31
你是说编程相互转换还是数学上的相互转换啊.问题不是很明了.