我有一个bitstring.Bitarray,想从某个位置读取到另一个位置。
我在for循环中有int可变长度,所以例如:length = 2
我的Bitarray看起来像:
msgstr = bitstring.BitArray(0b11110011001111110)
id = bitstring.BitArray()
m = 0
while 5 != m:
/////////////
Length changes in value part of Code
/////////////
x = 0
if m == 0:
while length != x:
id.append = msgstr[x] #msgstr is the BitArray that needs to be read
x = x + 1
m = m + 1
然后,我想读取前两位并将其转换为int,这样我就可以了:
id == 3
对于长度改变了值的下一轮,它应该从第三位开始,依此类推。
最佳答案
循环中的代码仅在m == 0时才执行任何操作,但是会增加m,因此循环中的第一次m仅为0。在其余时间中,您似乎没有做任何事情。
还有,你在哪里说
id.append = msgstr[x]
你可能真的想要
id.append(msgstr[x])
似乎您也可以从使用Python的slice notation中受益。