#include
#include
#include
#include
using namespace std;
static char binTohex(string temp);
static string binTohexstr(string temp);
void TestDouble(double value);
int main()
{
TestDouble(38414.4);
return 0;
}
void TestDouble(double value)
{
int count=0;
string x;
if (value>0)
{
x+="0"; //判断符号
}
else
{
x+="1";
value=-value;
}
while (2<=value) //获得小数点后值
{
value=value/2.0;
count++;
}
count=count+1023; //将阶码变成二进制表示
bitset<11>code(count);
x+=code.to_string(); //前半部分二进制表示
int digit=-1;
value-=1.0;
//将数值用二进制表示/
double posval=0.0;
double tempval=0.0;
while (value!=0&&digit>-54)
{
// posval=Power(2,digit);
posval=pow(2,digit);
tempval=value-posval;
if(tempval>0)
{
x+="1";
value=tempval;
}
else if (tempval==0)
{
x+="1";
break;
}
else
x+="0";
--digit;
}
int size=64-x.size();
if (size>0)
{
char* temp=new char[size];
memset(temp,'0',size);
x.append(temp,size);
delete temp;
}
cout<
}
static string binTohexstr(string temp)
{
string value="";
if (temp.size()%4!=0)
{
return NULL;
}
while ((temp.size()-4)>0)
{
value+=binTohex(temp.substr(0,4));
temp=temp.substr(4,temp.size()-4);
}
value+=binTohex(temp);
return value;
}
static char binTohex(string temp)
{
if("0000"==temp)
return '0';
else if("0001"==temp)
return '1';
else if("0010"==temp)
return '2';
else if ("0011"==temp)
return '3';
else if("0100"==temp)
return '4';
else if("0101"==temp)
return '5';
else if("0110"==temp)
return '6';
else if("0111"==temp)
return '7';
else if("1000"==temp)
return '8';
else if("1001"==temp)
return '9';
else if("1010"==temp)
return 'A';
else if("1011"==temp)
return 'B';
else if("1100"==temp)
return 'C';
else if("1101"==temp)
return 'D';
else if("1110"==temp)
return 'E';
else if("1111"==temp)
return 'F';
else
return 'G';
}
在测试过程中,发现这个函数有缺陷。譬如0.02转换的时候报错,现在修改如下:
#include
#include
#include
#include
using namespace std;
static char binTohex(string temp);
static string binTohexstr(string temp);
void TestDouble(double value);
int main()
{
TestDouble(22.02);
return 0;
}
void TestDouble(double value)
{
int count=0;
string x;
if (value>0)
{
x+="0"; //判断符号
}
else
{
x+="1";
value=-value;
}
while (2<=value) //获得小数点后值
{
value=value/2.0;
count++;
}
while(value<1)
{
value=value*2;
count--;
}
count=count+1023;
//
bitset<11>code(count);//将阶码变成二进制表示
x+=code.to_string();//前半部分二进制表示
int digit=-1;
value-=1.0;
//将数值用二进制表示/
double posval=0.0;
double tempval=0.0;
while (value!=0&&digit>-54)
{
// posval=Power(2,digit);
posval=pow(2,digit);
tempval=value-posval;
if(tempval>0)
{
x+="1";
value=tempval;
}
else if (tempval==0)
{
x+="1";
break;
}
else
x+="0";
--digit;
}
int size=64-x.size();
if (size>0)
{
char* temp=new char[size];
memset(temp,'0',size);
x.append(temp,size);
delete temp;
}
cout<
}
static string binTohexstr(string temp)
{
string value="";
if (temp.size()%4!=0)
{
return NULL;
}
while ((temp.size()-4)>0)
{
value+=binTohex(temp.substr(0,4));
temp=temp.substr(4,temp.size()-4);
}
value+=binTohex(temp);
return value;
}
static char binTohex(string temp)
{
if("0000"==temp)
return '0';
else if("0001"==temp)
return '1';
else if("0010"==temp)
return '2';
else if ("0011"==temp)
return '3';
else if("0100"==temp)
return '4';
else if("0101"==temp)
return '5';
else if("0110"==temp)
return '6';
else if("0111"==temp)
return '7';
else if("1000"==temp)
return '8';
else if("1001"==temp)
return '9';
else if("1010"==temp)
return 'A';
else if("1011"==temp)
return 'B';
else if("1100"==temp)
return 'C';
else if("1101"==temp)
return 'D';
else if("1110"==temp)
return 'E';
else if("1111"==temp)
return 'F';
else
return 'G';
}
感谢
至于原理我不是很明白,麻烦看这里。