在PHP中按位解包
我想通过8-8-8-7位的怪异序列将二进制字符串解压缩为数组.
I want to unpack a binary string into into an array by a weird sequence of 8-8-8-7 bits.
对于正常的8-8-8-8序列,我可以轻松地执行以下操作:
I could easily do something like this, for a normal 8-8-8-8 sequence:
$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分组.
That would give me a 2D array of bytes, grouped by 4.
但是由于第四位是7位,所以我想不出什么合适的方法.
But as the fourth being 7 bits, I just can't think of anything appropriate.
你有一些想法吗?
不确定我是否完全理解,但是如果您以未对齐/未填充的格式打包数据,则将需要使用某种比特流.
Not sure if I completely understand, but if you have packed data in an unaligned / unpadded format, you will want to use some sort of bitstream.
这是一个简单的类.理想情况下,它将是某种接受资源流的迭代器,但是直接显示如何通过字符串执行此操作会更简单:
Here's a simple class that does that. Ideally it would be some sort of iterator that accepts a resource stream, but showing how to do it via a string directly is simpler:
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位,则它会更快,但是按原样理解它要简单得多.
The readBits()
function would be faster if it were optimized to read up to 8 bits at a time, but it's much simpler to understand as-is.