views:

48

answers:

3

Hello,

I find myself in need of performing bit-level conversion on variables in PHP. In more detail, I have a bit stream that is read as an integer by hardware, and I need to do some operations on the bits to make it into what its actually supposed to be (a float). I have to do this a few times for different formats, and the functionality I need is

  • Being able to select and move individual bits in a variable
  • Being able to cast statically one type of variable to the other (ie. int to float)

I know php natively supports bitwise AND, OR, etc, and shift operations, but I was wondering if:

  • there may already be a library in php that does this sort of thing
  • I would be better off with delegating the calculations to some other language

Thanks,

+2  A: 

Is it pack and unpack that you want?

baloo
A: 

For converting bit stream to whatever you need use unpack().

Once converted to integer, you can use bitwise operators. Note, that they don't support floats, so in case of float it'd first be casted to integer.

vartec