7

我有以下代码:

$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 位整数?

4

1 回答 1

20

将其另存为两个 32Bit:

$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; 
于 2013-01-18T19:16:38.263 回答