打包64位整数

时间:2013-01-18 19:00:04

标签: php

我有以下代码:

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

1 个答案:

答案 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;