我有以下代码:
$packed = pack('i',PHP_INT_MAX);
echo unpack('i', $packed)[1];
结果我得到-1
我正在使用PHP 5.4.6-1ubuntu1.1 (cli) (built: Nov 15 2012 01:18:34)
我的PHP_INT_MAx
等于9223372036854775807
有没有办法处理pack
函数和64位整数?
答案 0 :(得分:19)
将其另存为两个32位:
$value = PHP_INT_MAX;
$highMap = 0xffffffff00000000;
$lowMap = 0x00000000ffffffff;
$higher = ($value & $highMap) >>32;
$lower = $value & $lowMap;
$packed = pack('NN', $higher, $lower);
list($higher, $lower) = array_values(unpack('N2', $packed));
$originalValue = $higher << 32 | $lower;