16进制转double dotnet_IEEE 16进制字符串转化为double类型

因项目需要,需要将内存中的64位16进制字符串表示成double。如下:

#include

#include

#include

#include

#include

using namespace std;

string charToBin(char temp);//十六进制转二进制串

int stringToDouble(string temp);//二进制串到double(整数)--求阶码

double BenToDex(string temp);//二进制串到double(小数)

double HexToDouble(string temp);//十六进制字符串到double类型

int main()

{

double a=HexToDouble("40e2c1cccccccccd");

cout<

return 0;

}

double HexToDouble(string temp)

{

string S_Bin="";//转化后的二进制字符串

for (int i=0;i

{

char temp1=temp.at(i);

S_Bin=S_Bin+charToBin(temp1);

}

int sign=0;//符号位

if (S_Bin.at(0)=='1')

{

sign=1;

}

string exponent="";

for (i=1;i<12;i++)

{

if (S_Bin.at(i)=='1')

{

exponent=exponent+'1';

}

else

exponent=exponent+'0';

}

int exponent_double=0;//阶码

exponent_double=stringToDouble(exponent);

exponent_double=exponent_double-1023;

string mantissa_temp="";

for (i=12;i<64;i++)

{

if (S_Bin.at(i)=='1')

{

mantissa_temp=mantissa_temp+'1';

}

else

mantissa_temp=mantissa_temp+'0';

}

double mantissa=0;

mantissa=BenToDex(mantissa_temp);

mantissa=mantissa+1.0;

double res=0;

double a,c;

a=pow((-1),sign);

c=pow(2,exponent_double);

res=a*mantissa*c;

return res;

}

string charToBin(char temp)//十六进制转二进制串

{

switch (temp)

{

case '0':

return "0000";

break;

case '1':

return "0001";

break;

case '2':

return "0010";

break;

case '3':

return "0011";

break;

case '4':

return "0100";

break;

case '5':

return "0101";

break;

case '6':

return "0110";

break;

case '7':

return "0111";

break;

case '8':

return "1000";

break;

case '9':

return "1001";

break;

case 'A':

case 'a':

return "1010";

break;

case 'B':

case 'b':

return "1011";

break;

case 'C':

case 'c':

return "1100";

break;

case 'D':

case 'd':

return "1101";

break;

case 'E':

case 'e':

return "1110";

break;

case 'F':

case 'f':

return "1111";

break;

default:

return "WRONG!";

}

}

int stringToDouble(string temp)//二进制串到double(整数)

{

double res=0;

for (int i=0;i

{

res=res*2+(temp[i]-'0');

}

return res;

}

double BenToDex(string temp)//二进制串到double(小数)

{

int m=temp.length();

double res=0;

for (int i=0;i

{

res=res+(temp[i]-'0')*pow(2,-i-1);

}

return res;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值