Get the least significant bit in PHP with Hex

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
// Read file, remove spaces
$file = implode('', explode(' ', file_get_contents('vogel.hex')));

// Save LSB
$bits = array();
for ($i = 1; $i < strlen($file); ++$i)
{
    $bits[] = $file[$i] & 1;
}

// Split LSB array into chunks of 8 bits.
$bytes = array_chunk($bits, 8);

// Implode byte arrays, convert to decimal, convert to ASCII.
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.

+5
3

for, :

$bits = '';
for ($i = 1; $i < strlen($file); $i++) {
    $bits .= (($file[$i] & 1) == 0) ? '0' : '1';
    if ($i % 8 == 0) {
       echo bindec($bits);
       $bits = '';
    }
}

, , 8.

+1

/2 ?

$hex = 0x05;
$shiftVal = (0 + $hex)/2;
echo $hex>>$shiftVal;//should output 1

, , :

$hex = 0xad;
echo $hex%2;
+1

, ASCII EBCDIC. , PHP.

, . , . , , ASCII/EBCDIC, .

+1

All Articles