duange051858 2015-12-03 13:23
浏览 427

将64位十六进制转换为PHP中的float

I'm trying to convert a 64 bit hexadecimal number to a float in PHP.

40F82C719999999A

If I run that in the IEEE-754 Floating-Point Conversion page at http://babbage.cs.qc.cuny.edu/IEEE-754.old/64bit.html it converts to:

99015.100000000000

Which is the number I'm looking for. But I can't get to this number in PHP. I've tried using various combinations of pack() and unpack() but I'm not anywhere close. :(

  • 写回答

2条回答 默认 最新

  • douyi6168 2016-01-09 07:17
    关注
    function hex2float($strHex) {
        $hex = sscanf($strHex, "%02x%02x%02x%02x%02x%02x%02x%02x");
        $hex = array_reverse($hex);
        $bin = implode('', array_map('chr', $hex));
        $array = unpack("dnum", $bin);
        return $array['num'];
    }
    
    $float = hex2float('40F82C719999999A');
    echo $float;
    

    will return 99015.1

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line