PHP按位遍历二进制文件

2016-03-31 11:00:00 947 次浏览

处理按位标识数据的二进制文件
/**
 * Created by PhpStorm.
 * User: samlv
 * Date: 2016/3/30
 * Time: 20:44
 */
$map = [];
$file = __DIR__.'/../report_file/nonSmallData';
$fp = fopen($file,'r');
if($fp){
    $bi = 12;
    $bufSize = 1 << $bi;
    for($n = 0;$bytes = fread($fp,$bufSize);$n++){
        $length = strlen($bytes);
        for($i = 0; $i < $length; $i++){
            $byte = hexdec(bin2hex($bytes[$i]));
            for($j = 0; $j < 8; $j++){
                if($byte & (0x80 >> $j)){
//                if($byte & (1 << $j)){    // 对每个字节反向读取
                    $id = ($n << $bi) + ($i << 3) + $j;
                    $map[$id] = isset($map[$id]) ? $map[$id] + 1 : 1;
                }
            }
        }
    }
    fclose($fp);
}
print_r($map);
echo 'count:'.count($map)."\n";

评论