Re: unpack() big endian signed long?
| From: | Nathan Nobbe | Date: | Wed, 23 Jan 2008 20:57:03 +0000 |
| Subject: | Re: unpack() big endian signed long? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-267935@lists.php.net to get a copy of this message | ||
On Jan 23, 2008 3:38 PM, Floor Terra <floort@gmail.com> wrote:
> Hi,
>
> Is it possible to use unpack() to read a big endian signed long
> on a little endian machine?
> http://nl.php.net/pack refers to perl, on wich this function is
> based.
> The php function unpack() doesn't seem to support the "<" and
> ">"
> modifiers like perl does.
did you see this in the comments on that page?
*info at dreystone dot com*
04-May-2005 02:31
<http://us.php.net/manual/en/function.unpack.php#52527>
Here is my
solution to reading a Big-Endian formatted double on an
Little-Endian machine.
<?php
function ToDouble($data) {
$t = unpack("C*", pack("S*", 256));
if($t[1] == 1) {
$a = unpack("d*", $data);
} else {
$a = unpack("d*", strrev($data));
}
return (double)$a[1];
}
?>
-nathan