RE: [PHP] Forcing little endian with pack("d");

2002-03-22 Thread Mika Tuupola

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/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Forcing little endian with pack("d");

2002-03-22 Thread Mike Eynon



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.

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?  Usually, it is
best to marshall the data in the native format of the machine doing the
marshalling, and then convert the data (if needed) on the machine you are
un-marshalling on.  This usually makes debugging a whole heck of a lot
easier.  All you will have to do is store the endian-ness of the machine you
are packing on with the data being packed.  Of course, this only works if
you are not unpacking with PHP...

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.

Good Luck.

// Mike Eynon
// www.MikeEynon.com
// 1366 Bulb Ave.
// Santa Cruz, CA  95062
// 831.588.2388

-Original Message-
From: Mika Tuupola [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 20, 2002 3:08 PM
To: [EMAIL PROTECTED]
Subject: [PHP] Forcing little endian with pack("d");



http://www.php.net/manual/en/function.pack.php

The manual page describes format string "d" as:

d double (machine dependent size and representation).

Is there an easy way of forcing the binary to be representated
little endian even with hosts which are big endian (undocumented
format string for example?).


--
Mika Tuupola  http://www.appelsiini.net/~tuupola/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php