16进制转double dotnet_将一个double转换为IEEE内存中16进制

#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';

}

感谢

至于原理我不是很明白,麻烦看这里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值