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

6
  • Why did you use S when your string seemingly requires big-endian unpacking?
    – mario
    Commented Sep 6, 2014 at 21:29
  • S = unsigned short (2 bytes)
    – asim-ishaq
    Commented Sep 6, 2014 at 21:30
  • 1
    S = (machine byte order).
    – mario
    Commented Sep 6, 2014 at 21:31
  • Then what should be used instead?
    – asim-ishaq
    Commented Sep 6, 2014 at 21:32
  • 2
    'n' php.net/manual/en/function.pack.php Commented Sep 6, 2014 at 21:33

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.