Posted by Mathias K. on 03/11/07 16:52
Hello everyone!
I'm currently dealing a lot with binary data.
Therefore it is sometimes that i have to read or write
single bits. What is the best way to do that?
For example let it be one single byte where the first 5 bits
contain a certain information, and the rest belongs to the next
binary block.
Now i get those 5 bits as follows: <<<PHP
// $fp is a filepointer to an open binary file
$byte = fread($fp, 1); // one byte
$bin = decbin( ord( $byte ) );
$fivebits = bindec( substr($byte, 0, 5) );
PHP;
Well that seems kind of long winded to me.
Isn't there are better, quicker and more elegant way to do that in PHP?
Because right now i don't know of any.
Thanks for any hints in advance.
~ MK
[Back to original message]
|