PHP- 2 bytes into 1 array

2019-08-24 04:04发布

I have printed out this binary which is 8 per line but yet to be stored in an array.

    <?php
// get contents of a file into a string
$filename = "rock.wav";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));


for($i = 0; $i < strlen($contents); $i++) {
    $char = $contents[$i];

    $str = ord ($char);

    echo str_pad(decbin($str), 8,"0",STR_PAD_LEFT)."<br/>";    


}
?>

The outcome is this:

01010010
01001001
01000110
01000110
00000010

it actually has more likely ten thousand line.

I want to read them into array which is looking like this,

arr[0] = 0000011010101101
arr[1] = 0000000010101101
arr[2] = 0000011010101101
arr[3] = 0000000010101101
arr[4] = 0000011010101101
arr[5] = 0000000010101101
arr[6] = 0000000010101101
arr[7] = 0000011010101101
arr[8] = 0000000010101101
arr[9] = 0000011010101101

which means, i want to store 2 bytes in 1 array, so that i can subtitute new bit into the last bit of the byte.

Any ways to do this? Do i need to use loop?

0条回答
登录 后发表回答