dqeznd1697 2016-02-19 21:41
浏览 809

在PHP中将IEEE754转换为十六进制

For a project I need to read in information from MQTT. The payload is filled with protobuf information, that needs to be converted.

For a certain value I receive 5.6904566139035E-28 as float. Using http://www.exploringbinary.com/floating-point-converter/ I can convert this when I tick single and raw hexadecimal value, then I receive 12345678, the value I should have (I know what is sent).

But now I need to do that conversion in PHP. I haven't any idea how this could be done. After some reading I figured out it is a Floating Point, but how to convert this like done on that website.

Is there someone that can help me with this!

Thanks a lot!

  • 写回答

1条回答

  • doumaogui5937 2016-02-20 01:02
    关注

    With the quite cryptic pack and unpack functions, it can be done in a one-liner:

    function rawSingleHex($num) {
        return strrev(unpack('h*', pack('f', $num))[1]);
    }
    

    This "packs" the number as its binary representation, then "unpacks" it in an array with one element: the binary representation in hexadecimal format. This format has the digits in the reversed order, so the function reverses that in the final result.

    Call it by passing the floating point number:

    echo rawSingleHex(5.6904566139035E-28);
    

    Output:

    12345678

    Without pack/pack

    (this was my original answer, but with the first option being available, this is not the advised way to proceed)

    The binary format is explained in Wikipedia's article on the Single-precision floating-point format.

    Here is a PHP function that implements the described procedure:

    function rawSingleHex($num) {
        if ($num == 0) return '00000000';
        // set sign bit, and add another, higher one, which will be stripped later 
        $sign = $num < 0 ? 0x300 : 0x200;
        $significant = abs($num);
        $exponent = floor(log($significant, 2));
        // get 24 most significant binary bits before the comma: 
        $significant = round($significant / pow(2, $exponent-23));
        // exponent has exponent-bias format:
        $exponent += 127;
        // format: 1 sign bit + 8 exponent bits + 23 significant bits,
        //         without left-most "1" of significant
        $bin = substr(decbin($sign + $exponent), 1) . 
               substr(decbin($significant), 1);
        // assert that result has correct number of bits:
        if (strlen($bin) !== 32) {
            return "unexpected error";
        }
        // convert binary representation to hex, with exactly 8 digits
        return str_pad(dechex(bindec($bin)), 8, "0", STR_PAD_LEFT);
    }
    

    It outputs the same as in the first solution.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?