RE: [PHP] Forcing little endian with pack("d");
| From: | Mika Tuupola | Date: | Fri, 22 Mar 2002 19:39:04 +0000 |
| Subject: | RE: [PHP] Forcing little endian with pack("d"); | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-89731@lists.php.net to get a copy of this message | ||
On Fri, 22 Mar 2002, Mike Eynon wrote:
> I have looked through the pack code in php-4.1.2. There appear to be no
> other options other than what is documented on the php.net pack function
> page. There appears to be no way of specifying endian-ness for floats or
> doubles using pack. They are copied to the output stream using a simple
> memcpy.
The solution was much simpler than I though. Reversing
the output with strrev() seems to do the job:
$double = pack("d", $val);
if ($bigendian) {
$double = strrev($double);
}
Now the value of $double is the same in big and smallendian
machines. So I suppose strrev() is binary safe.
> I would need more details on what you are doing to be sure, but, is the
> format something you can worry about on the unpacking side?
I am writing a binary file (BIFF) whose specs state that the data
needs to be written littleendian.
> The code in pack.c for the pack function is pretty dang simple. It wouldn't
> take more than an hour or so to cut and paste a few sections of code to add
> the options you need. Could be good to have in future versions of php.
+1 for adding that to future versions. Don't talk C myself
though ;)
--
Mika Tuupola http://www.appelsiini.net/~tuupola/