解包8字节整数

时间:2014-06-09 05:43:55

标签: php

我试图升级一个8字节整数,就像解压缩后面的4个字节一样:

print_r(unpack("N", "ACD0"));//just as expected, I get 1094927408

8字节:

$value = "ACD0DSAS";//I expect this to be unpacked in one 8 byte number
$higher = unpack("N", substr($value, 0, 4));
$lower = unpack("N", substr($value, 4, 4));
echo //the number

有没有办法在PHP中执行此操作?

1 个答案:

答案 0 :(得分:2)

这样的东西?

$value = "ACD0DSAS"; //I expect this to be unpacked in one 8 byte number
$higher = unpack("N", substr($value, 0, 4));
$lower = unpack("N", substr($value, 4, 4));

echo ($higher[1] << 32) + $lower[1]; // 4702677410000355667

使用bcmath

echo bcadd(bcmul($higher[1], bcpow(2, 32)), $lower[1]); // 4702677410000355667

没有解压缩:

echo hexdec(bin2hex($value)); // 4702677410000355667
// echo bindec($value); // does not work, i do not know why.

但结果将在32个系统上转换为float。 Float可以存储最大值9,007,199,254,740,992而不会丢失。