2017-04-25 31 views
1

我有一個bitstring.Bitarray,並希望從某個位置讀取到另一個位置。 我有一個for循環int變量長度,所以,比如我有:閱讀位定義的位置definded長度

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 併爲下一輪時,長度值發生了變化,應該從第三位等

回答

1

你的循環內部的代碼只能執行任何事情,如果m == 0,但你增加m,所以m通過循環第一次只有0。你經歷循環的其餘時間,似乎並沒有做任何事情。

而且,當你說

id.append = msgstr[x] 

你可能真的想

id.append(msgstr[x]) 

它也好像你可以使用Python的slice notation受益。

+0

謝謝,切片效果很好 –