note 100518 modified in function.pack by danbrown
| From: | danbrown@php.net | Date: | Thu, 21 Oct 2010 05:00:14 +0000 |
| Subject: | note 100518 modified in function.pack by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173330@lists.php.net to get a copy of this message | ||
If you need to unpack a signed short from big-endian or little-endian specifically, instead of
machine-byte-order, you need only unpack it as the unsigned form, and then if the result is >=
2^15, subtract 2^16 from it.
And example would be:
<?php
$foo = unpack("n", $signedbigendianshort);
$foo = $foo[1];
if($foo >= pow(2, 15)) $foo -= pow(2, 16);
?>
--was--
If you need to unpack a signed short from big-endian or little-endian specifically, instead of
machine-byte-order, you need only unpack it as the unsigned form, and then if the result is >=
2^15, subtract 2^16 from it.
And example would be:
$foo = unpack("n", $signedbigendianshort);
$foo = $foo[1];
if($foo >= pow(2, 15)) $foo -= pow(2, 16);
http://php.net/manual/en/function.pack.php