I have stumbled across a really odd issue in PHP that I thought would have been solved a very long time ago but doesn't seem to be.
I am reading a file of a proprietary format, byte by byte using
$handle = fopen("myfile.bin", "rb");
while (!feof($handle) && $pointer < $bytesToRead){
$c = fread($handle, 1);
$pointer ++;
if($c == 0xff){
echo "Desired functionality";
}
}
But the issue I have is that fread($handle, 1)
produces data in string format even though it knows I am reading a single byte of data.
Can I read it into a variable as an integer immediately so I can perform logical operations like $c == 0xff
or $c == 0b11111111
?
I'm running into a ridiculous usage of memory because I have to convert each byte read into a char array, convert those chars into integers and then perform my bitwise operators which involve rotation in that format. Then for readable output I have to go back to strings and chars with other odd functions in PHP which were introduced in 5.4
It all seems very weird.