I need a buffer (list of bytearray) with a length of 50 (nodes in my network, irrelevant), but I need exactly 8 bits to represent the byte array, what I have now is:
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)
What I am doing now is setting the leftmost position to 1 so I get exactly 8 bits, I need this layout for a certain decoding process. My question is : is there a more efficient way to have a list like that? i.e: A list of 50 bytearrays, with each byte array set to the index 1 (node.id). I want to omit the ones in the beginning yet still want the data to be represented in exactly 8 bits.
output:
10000000 bytearray(b'\x80') 10000001 bytearray(b'\x81') 10000010 bytearray(b'\x82') 10000011 bytearray(b'\x83') 10000100 bytearray(b'\x84') 10000101 bytearray(b'\x85') 10000110 bytearray(b'\x86') 10000111 bytearray(b'\x87') 10001000 bytearray(b'\x88') 10001001 bytearray(b'\x89') 10001010 bytearray(b'\x8a') 10001011 bytearray(b'\x8b') 10001100 bytearray(b'\x8c') 10001101 bytearray(b'\x8d') 10001110 bytearray(b'\x8e') 10001111 bytearray(b'\x8f') 10010000 bytearray(b'\x90') 10010001 bytearray(b'\x91') 10010010 bytearray(b'\x92') 10010011 bytearray(b'\x93') 10010100 bytearray(b'\x94') 10010101 bytearray(b'\x95') 10010110 bytearray(b'\x96') 10010111 bytearray(b'\x97') 10011000 bytearray(b'\x98') 10011001 bytearray(b'\x99') 10011010 bytearray(b'\x9a') 10011011 bytearray(b'\x9b') 10011100 bytearray(b'\x9c') 10011101 bytearray(b'\x9d') 10011110 bytearray(b'\x9e') 10011111 bytearray(b'\x9f') 10100000 bytearray(b'\xa0') 10100001 bytearray(b'\xa1') 10100010 bytearray(b'\xa2') 10100011 bytearray(b'\xa3') 10100100 bytearray(b'\xa4') 10100101 bytearray(b'\xa5') 10100110 bytearray(b'\xa6') 10100111 bytearray(b'\xa7') 10101000 bytearray(b'\xa8') 10101001 bytearray(b'\xa9') 10101010 bytearray(b'\xaa') 10101011 bytearray(b'\xab') 10101100 bytearray(b'\xac') 10101101 bytearray(b'\xad') 10101110 bytearray(b'\xae') 10101111 bytearray(b'\xaf') 10110000 bytearray(b'\xb0') 10110001
CodePudding user response:
Assuming you want a list of length 50 with a single byte bytearray with value of index 0x80, this will do it:
l_of_ba = [bytearray([i 0x80]) for i in range(50)]