php - Unpack() delivers different results on different machines -


i have strange behavior unpack function. have packed string, stored longblob in mysql database. when read string , unpack it, gives me array, far good. when run on machine of values in array different.

when dump data mysql, equal on both machines.

unpacking done way:

$array = unpack("n*", $packed);  

$array should (and on 1 machine)

array (     [1]  => 179848175     [2]  => -16214255     [3]  => 179848175     [4]  => -16214255     [5]  => 179848175     [6]  => -16214255     [7]  => 179999949     [8]  => -16152916     [9]  => 179999277     [10] => -16168574 ... ) 

but on other machine 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 different.

i have tested on 3 different machines, on 2 fine, on 1 machine weird output.

one machine running php 5.6.3 (here ok), 2 machines running php 5.5.14 (on 1 ok, on other not)

the pack format n means unsigned long, means can't negative. however, storing negative values, , ones aren't being unpacked way want. php not have pack format machine-independent signed longs; supports packing them in machine byte order, may not compatible machine machine. you'll have make values signed yourself.

to convert array items signed values:

for ($i = 1; $i <= count($array); $i++) {     // check greater-than-32-bit environment,     // , check if number should negative     // (i.e., if high bit set in 32-bit notation).     if (php_int_size > 4 && $array[$i] & 0x80000000) {         // negative number unpacked unsigned         // long in greater-than-32-bit environment.         // subtract appropriate amount (max 32-bit         // unsigned long + 1) convert negative.         $array[$i] = $array[$i] - 0x100000000;     } }  var_dump($array); 

Comments