PHP- 2 bytes into 1 array

无人久伴 提交于 2019-12-11 15:32:59

问题


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?

来源:https://stackoverflow.com/questions/50324622/php-2-bytes-into-1-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!