0

How to unpack() the first structure in this list?

I want the second field as integer (or to say better as float since PHP doesn't support 64bit int)

user990827
  • 553
  • 1
  • 5
  • 10
  • 2
    PHP supports 64-bit integers. Just `unpack` doesn't. Read out two 32-bit values an combine them. (Can't test, no legacy installation handy.) – mario Oct 28 '11 at 09:35
  • 1
    possible duplicate of [PHP: pack / unpack 64bit int on 64bit architecture](http://stackoverflow.com/questions/3265285/php-pack-unpack-64bit-int-on-64bit-architecture) – mario Oct 28 '11 at 09:38

1 Answers1

0

Thanks to mario, I solved it like this:

$a = unpack("i", $this->read(4));
$b = unpack("i", $this->read(4));
$packet['file_length'] = $a[1] + $b[1] * 0x100000000;

Where $this->read() is a wrapper for:

mb_substr($data, $offset, $length, '8bit');
hakre
  • 193,403
  • 52
  • 435
  • 836
user990827
  • 553
  • 1
  • 5
  • 10
  • 1
    Interesting, that this should work. "Combining two 32bit" does not mean to just add them: `($a[1] << 32) + $b[1]`. You must shift the upper 32 bits 32 bits to the left, to make space for the lower 32bits. – KingCrunch Oct 29 '11 at 22:15
  • Your are right, however it doesn't work as you propose. I tried to convert the following data to integer (from hex editor): 62 EA 9F 33 01 00 00 00 That should be 5.161.085.538 (~5gb), but it spits out int(866118243). The Par2 doc says all integers are little endian (which means I must use 'V' in unpack, though 'I' results in same data...). Any idea?. – user990827 Oct 30 '11 at 08:20
  • Got it finally: `$packet['file_length'] = $a[1] + $b[1] * 0x100000000;` – user990827 Oct 30 '11 at 08:53
  • This will only work until you hit the precision of a float... you will then lose the least significant bits. If I'm not mistaken, you have significant 52 bits in an IEEE 64-bit floating point value, which is hopefully enough for your purposes :-) – Arc Nov 25 '11 at 09:17