我需要一個長度為 50 的緩沖區(bytearray 串列)(網路中的節點,無關緊要),但我正好需要 8 位來表示位元組陣列,我現在擁有的是:
buffer = []
for position, nodeID in enumerate(range(128,128 50)):
print(bin(int(bytearray([nodeID]).hex(), base=16)).lstrip('0b'))
buffer.append(bytearray([nodeID]))
print(buffer[-1])
print(buffer)
我現在正在做的是將最左邊的位置設定為 1,這樣我就得到了 8 位,我需要這個布局來進行某個解碼程序。我的問題是:有沒有更有效的方法來獲得這樣的串列?即:50 個位元組陣列的串列,每個位元組陣列設定為索引 1 (node.id)。我想省略開頭的那些,但仍然希望資料以 8 位表示。
輸出:
10000000 位元組陣列(b'\x80') 10000001 位元組陣列(b'\x81') 10000010 位元組陣列(b'\x82') 10000011 位元組陣列(b'\x83') 10000100 位元組陣列(b'\x81') 10000100 位元組陣列(b'\x81') \x85') 10000110 位元組陣列(b'\x86') 10000111 位元組陣列(b'\x87') 10001000 位元組陣列(b'\x88') 10001001 位元組陣列(b'\x89') 10001010\x87') 10001010位元組陣列位元組陣列(b'\x8b') 10001100 位元組陣列(b'\x8c') 10001101 位元組陣列(b'\x8d') 10001110 位元組陣列(b'\x8e') 10001111 位元組陣列(b'\x8f') 10010b' x90') 10010001 bytearray(b'\x91') 10010010 bytearray(b'\x92') 10010011 bytearray(b'\x93') 10010100 bytearray(b'\x94') 10010101 bytearray(b'\x92') 10010101 bytearray(b'\x92') 10010101 bytearray(b'\x92') (b'\x96') 10010111 位元組陣列(b'\x97') 10011000 bytearray(b'\x98') 10011001 bytearray(b'\x99') 10011010 bytearray(b'\x9a') 10011011 bytearray(b'\x9b') 10011100\x99 bytearray(b'\x99') 10011100\10c10c bytearray(b'\x9d') 10011110 bytearray(b'\x9e') 10011111 bytearray(b'\x9f') 10100000 bytearray(b'\xa0') 10100001 bytearray(b'\xa1') 101000barray xa2') 10100011 bytearray(b'\xa3') 10100100 bytearray(b'\xa4') 10100101 bytearray(b'\xa5') 10100110 bytearray(b'\xa6') 10100111 bytearray\x0107' (b'\xa8') 10101001 bytearray(b'\xa9') 10101010 bytearray(b'\xaa') 10101011 bytearray(b'\xab') 10101100 bytearray(b'\xac') 10101101 bytearray(b'\xaa') ') 10101110 bytearray(b'\xae') 10101111 位元組陣列(b'\xaf') 10110000 位元組陣列(b'\xb0') 10110001
uj5u.com熱心網友回復:
假設您想要一個長度為 50 的串列,其中包含一個值為 index 0x80 的單位元組位元組陣列,則可以這樣做:
l_of_ba = [bytearray([i 0x80]) for i in range(50)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368757.html
