I came up with the PHP code below which converts a binary string 00000010 to the string "representation" and then later I convert it back to the binary string, however it's not getting back the same original binary string 00000010. The code below works perfectly with 10000010, 11000010... but for some strange reason it does not work with 00000010. Why?
$binary = '00000010';
echo $binary . "\n";
$temp = pack('H*',dechex(bindec($binary)));
echo str_pad(decbin(ord($temp)),8,'0',STR_PAD_LEFT);
OUTPUT:
00000010
00100000
I know some string "representation" of some binary strings are not visible/printable chars, but I dont care printing those chars. I just want to convert it to that string "representation" and back, but it's not working. What I am doing wrong?
EDIT:
After great help of @Rocket Hazmat the code started working for some binaries but not for others. The code below, for example, does not look to work, it outputs 0 instead of 10101010.
$binary = '10101010';
$temp = pack('C',base_convert($binary,2,16));
echo base_convert(ord($temp),16,2);