Search code examples
phpunpackendiannessint32

Unpack signed int32 little-endian in PHP


I have a binary file that I'm trying to parse. A little part of the file has a set of coordinates (latitude and longitude). A small example could be as follow:

$data = "64DA7300 0CD5DFFF";

And I'm trying to see the integers but I have no luck yet.

$header = unpack("ilatitud/ilongitude", $data);
print_r($header);

I know the correct numbers should be: (7592548, -2108148), however the results are 1094988854, 808465207.

Any ideas? Thanks.


Solution

  • $data = "64DA7300 0CD5DFFF";
    

    Your data is not in binary, it is hex-encoded ascii. Make it binary first:

    $data = hex2bin(str_replace(" ", "", $data));
    

    then your unpack will work.

    Example