I have a file with a hexadecimal code, and I need to get all the least significant bits from each byte in the file, combine them, divide them into groups of 8 and then convert the bytes to ASCII. My problem is extracting LSB from each byte.
The hex file looks like this (but much longer):
58 00 00 1F 58 00 00 00 00 00 00 00 00 00 00 1C 22 23 1F 26 25 1E 2C 26 20 31 2B 22 38 2F 26 42 36 25 47 37 24 49 39 22
My code is as follows:
<?php
$file = implode('', explode(' ', file_get_contents('vogel.hex')));
$bits = array();
for ($i = 1; $i < strlen($file); ++$i)
{
$bits[] = $file[$i] & 1;
}
$bytes = array_chunk($bits, 8);
foreach ($bytes as $byte)
{
echo chr(bindec(implode('', $byte)));
}
?>
I think that part of the splitting and conversion should work correctly, but I think I was wrong when extracting the LSB. Can someone provide an example of how I can extract LSB?
I changed my code a bit to start reading bits at position 1. Then the decimal representation is within the ASCII range, and the script outputs the actual ASCII character.