我正在尝试压缩一个 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 中做到这一点?
最佳答案
是这样的吗?
$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 个值而不丢失。
关于php - 解压 8 字节整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114319/