我对解压功能有一些奇怪的行为。我有一个打包的字符串,作为longblob存储在mysql数据库中。当我读取该字符串并解压它时,它会给我一个数组,到目前为止还不错。但是当我在另一台机器上运行时,数组中的一些值是不同的。
当我从mysql转储数据时,它们在这两台机器上是相等的。
拆箱是这样做的:
$array = unpack("N*", $packed);
然后,$array
应该是这样的(而且它在一台机器上)
Array
(
[1] => 179848175
[2] => -16214255
[3] => 179848175
[4] => -16214255
[5] => 179848175
[6] => -16214255
[7] => 179999949
[8] => -16152916
[9] => 179999277
[10] => -16168574
...
)
但在另一台机器上是这样的:
Array
(
[1] => 179848175
[2] => 427853622
[3] => 179848175
[4] => 427853622
[5] => 179848175
[6] => 427853622
[7] => 179999949
[8] => 427853423
[9] => 179999277
[10] => 427853341
...
)
每一秒的价值似乎都不同。
我在三台不同的机器上测试了这个,在两台机器上一切都很好,但是在那台机器上我得到了奇怪的输出。
一台机器正在运行PHP5.6.3(这里是ok的),两台机器运行的是PHP5.5.14(其中一台运行正常,另一台不运行)。
发布于 2015-07-13 18:40:47
pack
格式N
表示无符号长,这意味着它不可能是负的。但是,您存储的是负值,而它们并不是按您所希望的方式解压的。PHP没有独立于机器的pack
格式;它只支持按机器字节顺序打包它们,而机器之间的字节顺序可能不兼容。因此,您必须自己签名这些值。
若要将数组项转换为有符号值,请执行以下操作:
for ($i = 1; $i <= count($array); $i++) {
// Check for a greater-than-32-bit environment,
// and check if the number should be negative
// (i.e., if the high bit is set in 32-bit notation).
if (PHP_INT_SIZE > 4 && $array[$i] & 0x80000000) {
// A negative number was unpacked as an unsigned
// long in a greater-than-32-bit environment.
// Subtract the appropriate amount (max 32-bit
// unsigned long + 1) to convert it to negative.
$array[$i] = $array[$i] - 0x100000000;
}
}
var_dump($array);
https://stackoverflow.com/questions/31390533
复制相似问题