You set $bin to integer 13
Using substr() for $bin is to pour $bin into a string ( "13" )
You are reading the first character of this line ( "1" )
Using printf() with %b , you explicitly return this string to integer 1
the argument is treated as an integer and is represented as a binary number.
EDIT
This code should give the result you expect
$bin = 0b00001101; // 13 - ASCII Carriage return $c = substr(chr($bin), 0, 1); // read this character printf("Expectation: 00001101, reality: %08b\n", ord($c)); // 00001101
source share