I have the following code:
<?php
$val = fread($fp,1);
echo 'bindec: '.bindec($val)."\n";
echo 'bin2hex: '.bin2hex($val)."\n";
echo 'bin2hex/hexdec: '.hexdec(bin2hex($val))."\n";
?>
The byte being read appears in my hex editor as 0F. My output looks like this:
bindec: 0
bin2hex: 0f
bin2hex/hexdec: 15
My question is, why is bindec returning 0? Surely it should return 15?
Use
ord()function instead ofbindec()in the first line.bindec()requires an input string containing only1or0like “110101”. it does not take “real” binary data as input.