note 76865 added to function.unpack
| From: | ein at anti-logic dot com | Date: | Thu, 02 Aug 2007 22:13:17 +0000 |
| Subject: | note 76865 added to function.unpack | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129136@lists.php.net to get a copy of this message | ||
If you need to unpack an unsigned long that's larger than php's limit (2^31-1) and
don't mind doing a lot of fiddling, an easy solution is to unpack it as two shorts.
For example
<?php
$val = 2147483647; // we can't actually assign a number larger than 2^31-1 in php to an int,
but if you're reading it from outside, it's easy to get larger than that.
// split it into two 16 bit words
$high= $val>>16;
$low = $val & 65536;
$packed= pack('nn',$high,$low);
// we have a 32bit long packed number assigned to $packed
$unpacked = unpack('nhigh\nlow',$packed);
$final = ($unpacked['high'] << 16) + $unpacked['low'];
?>
Of course this works equally as well as unpacking an externally created unsigned long.
This is most usefull when dealing with bitmasks, where the high and low word don't need to be
connected.
Working with signed values is also possible, but I don't have any code examples to share (no
need for them myself).
----
Server IP: 216.235.15.211
Probable Submitter: 204.112.176.136
----
Manual Page -- http://www.php.net/manual/en/function.unpack.php
Edit -- https://master.php.net/note/edit/76865
Del: integrated -- https://master.php.net/note/delete/76865/integrated
Del: useless -- https://master.php.net/note/delete/76865/useless
Del: bad code -- https://master.php.net/note/delete/76865/bad+code
Del: spam -- https://master.php.net/note/delete/76865/spam
Del: non-english -- https://master.php.net/note/delete/76865/non-english
Del: in docs -- https://master.php.net/note/delete/76865/in+docs
Del: other reasons-- https://master.php.net/note/delete/76865
Reject -- https://master.php.net/note/reject/76865
Search -- https://master.php.net/manage/user-notes.php