IEEE 16进制字符串转化为double类型
2010-07-28 20:03
363 查看
因项目需要,需要将内存中的64位16进制字符串表示成double。如下:
#include <iostream> #include <bitset> #include <cmath> #include <stdio.h> #include <string> 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<<a; return 0; } double HexToDouble(string temp) { string S_Bin="";//转化后的二进制字符串 for (int i=0;i<temp.length();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<temp.length();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<m;i++) { res=res+(temp[i]-'0')*pow(2,-i-1); } return res; }
相关文章推荐
- 【数据采集】将16进制字符串转化为Double类型输出(依照IEEE754标准)
- C语言:将16进制字符串转化为int类型值
- C语言:将16进制字符串转化为int类型值
- 十位以上长整型或double类型数强制转换为16进制字符串
- C++实现将一个字符串转化为double类型
- C语言:将16进制字符串转化为int类型值
- 16进制字符串转化成int类型
- 把字符串转化为类型
- string datdString='Wed Oct 12 2016 00:00:00 GMT+0800 (中国标准时间)'将字符串转化为date类型,格式2016-10-12
- java实现16进制字符串转化为long值
- 【C编程练习】2013华为校园招聘机试题目题5:将一个int类型的数字转化为字符串,并判断有无重复字符串(长度应大于2)有返回1,无返回2
- zoj3819Average Score 类型转化 <求大于1个(double)数的整数,小于1个double类型的数的整数>
- 把时间从字符串类型转化为Date类型
- 将未指定格式的日期字符串转化成java.util.Date类型日期对象
- 将科学计数法转化为常规格式再转化为字符串类型(String)
- C++int、float、double转化为string类型;string转化为int、float、double类型-1
- SqlServer 中Binary类型的数据如何转化为字符串
- C/C++中int/long/float/double数值类型与字符串互相转换
- 字符串类型间的转化
- 如何把字符串转化为ENUM类型?