• 【数据采集】将16进制字符串转化为Double类型输出(依照IEEE754标准)


    因为需要读取二进制文件中包含的数据,故需要这样一个转化过程。

    鄙人功力太浅,主要参照http://www.cnblogs.com/xinjun/archive/2010/07/28/1787297.html,略有改动,以保证编译运行通过。

      1 #include <iostream>
    2 #include <bitset>
    3 #include <cmath>
    4 #include <stdio.h>
    5 #include <string>
    6
    7 usingnamespace std;
    8
    9 string charToBin(char temp);//十六进制转二进制串
    10 int stringToDouble(string temp);//二进制串到double(整数)--求阶码
    11 double BenToDex(string temp);//二进制串到double(小数)
    12 double HexToDouble(string temp);//十六进制字符串到double类型
    13
    14 int main()
    15 {
    16 double a=HexToDouble("40AD5807C0000000");
    17 cout<<a;
    18 cin.get();
    19
    20 return0;
    21 }
    22
    23 double HexToDouble(string temp)
    24 {
    25 string S_Bin="";//转化后的二进制字符串
    26 for (int i=0;i<temp.length();i++)
    27 {
    28 char temp1=temp.at(i);
    29 S_Bin=S_Bin+charToBin(temp1);
    30 }
    31
    32 int sign=0;//符号位(1位)
    33 if (S_Bin.at(0)=='1')
    34 {
    35 sign=1;
    36 }
    37
    38 string exponent="";//获取阶码字符串(11位)
    39 for (int i=1;i<12;i++)
    40 {
    41 if (S_Bin.at(i)=='1')
    42 exponent=exponent+'1';
    43 else
    44 exponent=exponent+'0';
    45 }
    46
    47 int exponent_double=0;//阶码
    48 exponent_double=stringToDouble(exponent);
    49 exponent_double=exponent_double-1023;//减去偏移值
    50
    51
    52 string mantissa_temp="";//获取尾数字符串(52位)
    53 for (int i=12;i<64;i++)
    54 {
    55 if (S_Bin.at(i)=='1')
    56 mantissa_temp=mantissa_temp+'1';
    57 else
    58 mantissa_temp=mantissa_temp+'0';
    59 }
    60
    61 double mantissa=0;//尾数
    62 mantissa=BenToDex(mantissa_temp);
    63 mantissa=mantissa+1.0;
    64
    65 //双精度公式:
    66 // F=1.M(二进制)
    67 // V=(-1)^s*2^(E-1023)*F
    68 double res=0;
    69 double a,c;
    70 a=pow((-1.0),sign);
    71 c=pow(2.0,exponent_double);
    72 res=a*c*mantissa;
    73
    74 return res;
    75 }
    76
    77 string charToBin(char temp)//十六进制转二进制串
    78 {
    79 switch (temp)
    80 {
    81 case'0':
    82 return"0000";
    83 break;
    84 case'1':
    85 return"0001";
    86 break;
    87 case'2':
    88 return"0010";
    89 break;
    90 case'3':
    91 return"0011";
    92 break;
    93 case'4':
    94 return"0100";
    95 break;
    96 case'5':
    97 return"0101";
    98 break;
    99 case'6':
    100 return"0110";
    101 break;
    102 case'7':
    103 return"0111";
    104 break;
    105 case'8':
    106 return"1000";
    107 break;
    108 case'9':
    109 return"1001";
    110 break;
    111 case'A':
    112 case'a':
    113 return"1010";
    114 break;
    115 case'B':
    116 case'b':
    117 return"1011";
    118 break;
    119 case'C':
    120 case'c':
    121 return"1100";
    122 break;
    123 case'D':
    124 case'd':
    125 return"1101";
    126 break;
    127 case'E':
    128 case'e':
    129 return"1110";
    130 break;
    131 case'F':
    132 case'f':
    133 return"1111";
    134 break;
    135 default:
    136 return"WRONG!";
    137 }
    138 }
    139
    140 int stringToDouble(string temp)//二进制串到double(整数)
    141 {
    142 int res=0;
    143 for(int i=0;i<temp.length();i++)
    144 {
    145 res=res*2+(temp[i]-'0');
    146 }
    147 return res;
    148 }
    149
    150 double BenToDex(string temp)//二进制串到double(小数)
    151 {
    152 int m=temp.length();
    153 double res=0;
    154 for (int i=0;i<m;i++)
    155 {
    156 res=res+(temp[i]-'0')*pow(2.0,-i-1);
    157 }
    158 return res;
    159 }

      

  • 相关阅读:
    iterm2 配色修改
    让Dock自动 显示/隐藏 不再有延迟
    Xcode更改配色方案
    CocoaPods安装与使用
    CocoaPods安装和使用及问题:Setting up CocoaPods master repo
    CocoaPods安装和使用教程
    RubyGems 镜像
    iOS Mac系统下Ruby环境安装
    MAC机中安装RUBY环境
    Kibana+Logstash+Elasticsearch 日志查询系统
  • 原文地址:https://www.cnblogs.com/pezy/p/Hex2Double.html
Copyright © 2020-2023  润新知