views:

38

answers:

1

Is there any lib witch i can use to convert integers and floats between different architecture representation?

ie. -255 with big-endian 2byte signed integer is: 0xff7f and for 4 byte signed integer is 0xffffff7f and same with other...

I have found Binary tools package in PEAR, but it status is unmaintained and stopped at 0.3.0 version, i'm not sure if it will produce correct results.

[EDIT]

I know about function pack, but it's results like said in manual are machine dependent, witch is not the result i want to have

A: 

The pack function has, for integers, several modes that are not machine-dependent:

n   unsigned short (always 16 bit, big endian byte order)
v   unsigned short (always 16 bit, little endian byte order)
N   unsigned long (always 32 bit, big endian byte order)
V   unsigned long (always 32 bit, little endian byte order)

For floats, there's no such thing:

f   float (machine dependent size and representation)
d   double (machine dependent size and representation)

However, virtually all the systems will use IEEE 754 single precision for "float" and double precision for "d".

So, the only thing that's variable is endianness. You could check the endianness of the system where the system is running for instance doing:

$isLittle = pack('s', 1) == "\x01\x00")

And then reversing the endianness of the data with strrev, if appropriate.

Artefacto
"However, virtually all the systems will use IEEE 754 single precision for "float" and double precision for "d"." virtually? So if all will use this, why manual says "Machine dependent" explicite?
canni
@Dariusz Because PHP tries to follow the C89 standard, which didn't require IEEE 754 compliance (even C99 makes some stuff optional). But in practice, you only have to lookout for endianness.
Artefacto
Okay I'll trust You on this :)
canni