简体   繁体   中英

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

I have a float variable with this value: 8546033237

I want to convert it to a 64bit binary and use this way for convert it to binary:

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

this way give me a 32bit value not 64bit. then when I convert back it to number give me this:

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

result is : float 8546033152

result isnt equal with initial value.

now how convert it to a 64bit binary and vice versa correctly ??

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

I used this way for converting 32bit float to 64bit binary:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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