[英]Unpack() delivers different results on different machines
I have some strange behavior with the unpack function. 解包函数我有一些奇怪的行为。 I have a packed string, stored as longblob in a mysql database. 我有一个打包的字符串,在mysql数据库中存储为longblob。 When I read that string and unpack it, it gives me an array, so far so good. 当我读取该字符串并将其解压缩时,它给了我一个数组,到目前为止一切都很好。 But when I run this on another machine some of the values in the array are different. 但是当我在另一台机器上运行它时,数组中的一些值是不同的。
When I dump the data from mysql, they are equal on both machines. 当我从mysql转储数据时,它们在两台机器上都是相同的。
Unpacking is done this way: 解包是这样完成的:
$array = unpack("N*", $packed);
$array
should then be like this (and it is on one machine) $array
应该是这样的(它在一台机器上)
Array
(
[1] => 179848175
[2] => -16214255
[3] => 179848175
[4] => -16214255
[5] => 179848175
[6] => -16214255
[7] => 179999949
[8] => -16152916
[9] => 179999277
[10] => -16168574
...
)
But on the other machine it is like this: 但在另一台机器上它是这样的:
Array
(
[1] => 179848175
[2] => 427853622
[3] => 179848175
[4] => 427853622
[5] => 179848175
[6] => 427853622
[7] => 179999949
[8] => 427853423
[9] => 179999277
[10] => 427853341
...
)
Every second value seems to be different. 每一秒的价值似乎都不同。
I have tested this on three different machines, on two everything was fine, but on that one machine I get that weird output. 我已经在三台不同的机器上对它进行了测试,两台机器都很好,但是在那台机器上我得到了那个奇怪的输出。
One machine is running PHP 5.6.3 (here it is ok), two machines are running PHP 5.5.14 (on one it is ok, on the other not) 一台机器正在运行PHP 5.6.3(这里没问题),两台机器运行PHP 5.5.14(一台机器运行正常,另一台机器运行不正常)
The pack
format N
means unsigned long, which means it can't be negative. pack
格式N
表示无符号长,这意味着它不能为负数。 However, you are storing negative values, and they are the ones that aren't being unpacked the way you want. 但是,您存储负值,并且它们是未按您希望的方式解压缩的值。 PHP does not have a pack
format for machine-independent signed longs; PHP没有与机器无关的签名longs的pack
格式; it only supports packing them in machine byte order, which may not be compatible from machine to machine. 它只支持以机器字节顺序打包它们,这可能与机器不兼容。 Thus you'll have to make the values signed yourself. 因此,您必须自己签署值。
To convert your array items into signed values: 要将数组项转换为有符号值:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.