简体   繁体   English

如何将float转换为64bit二进制或将32bit float转换为64bit int-PHP

[英]how convert float to 64bit binary or 32bit float to 64bit int - php

I have a float variable with this value: 8546033237 我有一个浮点变量,其值为: 8546033237

I want to convert it to a 64bit binary and use this way for convert it to binary: 我想将其转换为64位二进制文​​件,并使用这种方式将其转换为二进制文件:

$a = 8546033237;
$b = pack("f",$a);

this way give me a 32bit value not 64bit. 这样给我的不是32位的32位值。 then when I convert back it to number give me this: 然后当我将其转换回数字时,请给我:

$a = unpack("f",$b);
var_dump($a);

result is : float 8546033152 结果是: 浮点数8546033152

result isnt equal with initial value. 结果不等于初始值。

now how convert it to a 64bit binary and vice versa correctly ?? 现在如何将其正确转换为64位二进制文​​件,反之亦然?

在手动function.pack上,您会得到答案#93085

I used this way for converting 32bit float to 64bit binary: 我使用这种方式将32位浮点数转换为64位二进制数:

function float2binary64($in){
        $k = '';
        while (!in_array($in,array(0,1))) {
            $k .= abs($in % 2);
            $in = floor($in / 2);
        }
        $k .= $in;

        $j = strlen($k);
        for($i=0; $i< (64-$j); $i++){
            $k .="0";
        }

        $j = 0;$l[$j] = '';$m=0;
        for($i=63; $i>= 0; $i--){
            if ($m == 32) {$j++; $l[$j] = '';$m=0;}

            $l[$j] .= $k[$i];
            $m++;
        }
        $high = bindec($l[0]);
        $low = bindec($l[1]);
        $bin = pack("N*",$high,$low);

        return $bin;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM