5

I am trying to convert a 2 byte string into a Short/int data type with unpack but it does not seem to work:

$str = "\x01\xBB";
unpack("S",$str);

it gives 47873 where as it must return 443

asim-ishaq
  • 2,190
  • 5
  • 32
  • 55

1 Answers1

6

You need to use n as the format string instead.

$str = "\x01\xBB";
unpack("n",$str);

Look here for more format options.
http://php.net/manual/en/function.pack.php

ElefantPhace
  • 3,806
  • 3
  • 20
  • 36