有效地重组二进制数据,然后转置块



我有一个原始的二进制文件,该文件保存32个样本,每个样本为256位。每个样品的第n个元素组合以创建一个有效的样本,因此在这种情况下256个有效样本。样品顺序在输入文件上不正确,必须重新排序。为此,我使用的是Bitstring模块并在8192位中读取,然后将它们拆开为正确的顺序。这是合理的。现在,我必须将所有样品的n位组合到一个新结构中,以使每个样本为32位。我正在问如何有效地重新订购(已完成),然后转置原始二进制数据。也许最好完全放弃Bortstring模块。

from bitstring import ConstBitStream, ReadError, BitArray
ch = [ConstBitStream('0x0000000000000000000000000000000000000000000000000000000000000000')
                      for i in range(32)]
test = "test"
fp = ConstBitStream(filename="test.jlb")
try:
    firstChunk = fp.read(8192)#read one sample    
    ch[28],ch[29],ch[17],ch[18],ch[19],ch[11],ch[14],ch[12],
    ch[31],ch[23],ch[13],ch[21],ch[20],ch[7],ch[24],ch[9],
    ch[8],ch[27],ch[10],ch[30],ch[22],nocare,nocare,ch[6],
    ch[4],ch[5],ch[3],ch[2],ch[1],ch[15],ch[0],nocare = firstChunk.unpack('32*bits:256')#put it in order
except ReadError:
    pass
blist = [element[3] for element in ch]#try to create new sample
print(blist)#looks good list of booleans
print(BitArray('').join(blist))#no good 0x00

构造 blist时,您会得到布尔值列表,而python只是整数(false == 0,true == 1)。

因此,当您尝试使用join时,它将从每个整数类型中创建一个新的BitArray对象,该对象被解释为"创建此长度的BitArray"。因此BitArray(False)是空的(零长),而BitArray(True)是一个零位!

这是相当违反直觉的,但是由于原因太长而无法进入这里。

要从布尔人列表中创建,只需直接执行此操作而不是使用加入:

print(BitArray(blist))

相关内容

最新更新