Page 1 of 1

Dealing with Binary Data

Posted: Fri Jul 31, 2009 6:17 am
by agravayne
Hello,

I am trying to get my head around dealing with binary data and PHP - not having great success.

At present I want to parse in binary data from a file in chunks. As an example this is my tet php.

Code: Select all

$filename="test.bin";
$handle=fopen($filename,"rb");
$byte=fread($handle,1);
fclose($handle);
echo "1st byte is $byte";
Now this reads the first byte from my test file. This is 01 when viewed in a Hex editor so Binary 00000001 and 1 in decimal. I know I am reading the value correctly - if I write it back into a new binary file it shows as 01 again so the vlue is correct. But the echo above display just a nonsense charcater. How do I convert the variable $byte into either a decimal value or a binary value that I can manipulate in PHP?

Many thanks
Scott

Re: Dealing with Binary Data

Posted: Fri Jul 31, 2009 8:39 am
by Mark Baker
ord() and chr()

Re: Dealing with Binary Data

Posted: Fri Jul 31, 2009 8:55 am
by agravayne
That would give me the equivalent ascii code and I want the numerical value. It doesn''t work anyway as chr needs the decimal number passed to it and the variable $byte is not a deciaml number its pure binary.

Re: Dealing with Binary Data

Posted: Fri Jul 31, 2009 8:58 am
by Eran