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? Commented Sep 6, 2014 at 21:29
  • S = unsigned short (2 bytes) Commented Sep 6, 2014 at 21:30
  • 1
    S = (machine byte order). Commented Sep 6, 2014 at 21:31
  • Then what should be used instead? 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.