简体   繁体   English

在python的字节数组中用8位表示int

[英]Representing int with 8 bits in python's byte array

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:我需要一个长度为 50 的缓冲区(字节数组列表)(我的网络中的节点,无关紧要),但我需要正好 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)

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.我现在正在做的是将最左边的 position 设置为 1,这样我就得到了 8 位,我需要这个布局来进行特定的解码过程。 My question is: is there a more efficient way to have a list like that?我的问题是:有没有更有效的方法来制作这样的列表? ie: A list of 50 bytearrays, with each byte array set to the index+1 (node.id).即:50 个字节数组的列表,每个字节数组设置为索引+1(node.id)。 I want to omit the ones in the beginning yet still want the data to be represented in exactly 8 bits.我想省略开头的那些,但仍然希望数据准确地以 8 位表示。

output: 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 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 字节数组(b'\x91') 10010010 字节数组(b'\x92') 10010011 字节数组(b'\x93') 10010100 字节数组(b'\x94') 10010101 字节数组(b'\x95') 10010110 字节数组(b'\x96') 10010111 字节数组(b'\x97') 10011000 字节数组(b'\x98') 10011001 字节数组(b'\x99') 10011010 字节数组(b'\x9a') 10011011 字节数组(b'\x9b ') 10011100 字节数组(b'\x9c') 10011101 字节数组(b'\x9d') 10011110 字节数组(b'\x9e') 10011111 字节数组(b'\x9f') 10100000 字节数组(b'\xa0') 10100001 字节数组( b'\xa1') 10100010 字节数组(b'\xa2') 10100011 字节数组(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 '\xa3') 10100100 字节数组(b'\xa4') 10100101 字节数组(b'\xa5') 10100110 字节数组(b'\xa6') 10100111 字节数组(b'\xa7') 10101000 字节数组(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 字节数组(b'\xaf') 10110000 字节数组(b'\xb0') 10110001

Assuming you want a list of length 50 with a single byte bytearray with value of index + 0x80, this will do it:假设您想要一个长度为 50 的列表,其中包含一个索引值为 + 0x80 的单字节字节数组,这将执行此操作:

l_of_ba = [bytearray([i + 0x80]) for i in range(50)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM