Does this do what you're looking for?
<?php
$output = "";
$filename = "/var/www/BB/file.ppm2";
$content = file_get_contents($filename);
$content = str_split($content, 8);
foreach($content as $char) {
$output .= chr(bindec($char));
}
?>
Question
I got a file with 2880 characters having ONLY 1
s and 0
s.
Each of the characters occupy one byte in the file.
We want to move chunks of 8 characters, treat it as a bit representation and move it as one byte into a new file. The result is then a file with the size of 1/8th of the original file.
So far I got:
$filename = "/var/www/BB/file.ppm2";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$content_read = substr(fread($handle, $fsize), 0, 8640);
for($i = 0; $i <360; $i++) {
$offset_8 = $i * 8;
$content_read_8 = substr($content_read, $offset_8, 8);
But how can I get the content of $content_read_8
(ex. 01101101
) into a one character byte: $byte_out ???
Thanks for you help ;-)
Solution
Does this do what you're looking for?
<?php
$output = "";
$filename = "/var/www/BB/file.ppm2";
$content = file_get_contents($filename);
$content = str_split($content, 8);
foreach($content as $char) {
$output .= chr(bindec($char));
}
?>
OTHER TIPS
You can use bitwise operations:
for($i = 0; $i <360; $i++) {
$offset_8 = $i * 8;
$content_read_8 = substr($content_read, $offset_8, 8);
$char = 0;
for ($j = 0; $j < 8; $j += 1) {
// Move all the bits 1 place to the left - its like sticking a 0 to the right.
$char << 1;
// If its a '1' char, you need to adjust that bit
if ($content_read_8[$j] == '1') {
$char += 1;
}
}
}
You can read more on PHP bitwise operators here