在PHP位解压(Unpack by bits in PHP)

2019-07-30 14:45发布

我想通过8-8-8-7比特的怪异序列二进制串解压缩到到一个数组。

我可以很容易地做这样的事情,对于一个正常的8-8-8-8顺序:

$b=unpack('C*',$data);
for ($i=0,$count=sizeof($b); $i < $count; $i+=4) {
$out[]=array($b[$i+1],$b[$i+2],$b[$i+3],$b[$i+4]);
}

这会给我的字节二维数组,由4分组。

但是,随着第四是7位,我只是想不出合适的东西。

你有一些想法?

Answer 1:

不知道如果我完全理解,但如果你已经不对齐/护垫与格式打包的数据,你会想使用某种比特流。

这里有一个简单的类,做到这一点。 理想情况下这将是某种形式的迭代器,它接受一个资源流,但显示了如何通过直接将字符串是简单做到这一点:

class BitStream
{
  private $data, $byte, $byteCount, $bytePos, $bitPos;
  private $mask = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80];

  public function __construct($data)
  {
    $this->data = $data;
    $this->byteCount = strlen($data);
    $this->bytePos = 0;
    $this->bitPos = 7;

    $this->byte = $this->byteCount ? ord($data[0]) : null;
  }

  // reads and returns 1 bit. null on no more bits
  public function readBit()
  {
    if ($this->byte === null) return null;

    // get current bit
    $bit = ($this->byte & $this->mask[$this->bitPos]) >> $this->bitPos;

    if (--$this->bitPos == -1)
    {
      // advance to next byte 
      $this->bitPos = 7;
      $this->bytePos++;
      $this->byte = $this->bytePos < $this->byteCount ? ord($this->data[$this->bytePos]) : null;
    }

    return $bit;
  }

  // reads up to $n bits, where 0 < $n < bit length of max int
  // returns null if not enough bits left
  public function readBits($n)
  {
    $val = 0;
    while ($n--)
    {
      $bit = $this->readBit();
      if ($bit === null) return null;      

      $val = ($val << 1) | $bit;
    }

    return $val;
  }
}

然后使用它:

$bs = new BitStream($data);

$out = [];
while (true)
{
  $a = $bs->readBits(8);
  $b = $bs->readBits(8);
  $c = $bs->readBits(8);
  $d = $bs->readBits(7);

  if ($d === null) break; // ran out of data

  $out[] = [$a, $b, $c, $d];
}

readBits()函数会更快,如果它进行了优化,同时读取多达8位,但它更易于理解原样。



文章来源: Unpack by bits in PHP