Page 1 of 1

How to read a binary file

Posted: Wed Mar 31, 2004 9:01 am
by eltrujas
Hello everybody,

I'm trying to read binary data from a file, lets say "info.dat". If you opened the file with a hexeditor, you would see something like the following (all values are in hexadecimal notation):

78 42 8c 09 aa f7 2b 67 90 5d

The first 4 bytes is an unsigned integer, which should be read like:
098C4278 = 160187000 in decimal

The next 6 bytes should be read as they are:
AA F7 2B 67 90 5D

The PHP code I need should print out the info like this:
"Code 1 = 160187000"
"Code 2 = AA F7 2B 67 90 5D"

Right now I am trying this code:
$filename = "info.dat";
$handle = fopen($filename, "rb");
$code1 = fread($handle, 4);
$code2 = fread($handle, 6);

if I do a : echo $code1; it outputs "xBŒ"

How can I solve this?

Thank you.