python - 将 2 个字节从字节数组转换为一个整数

我有一个 bytearray 列表,看起来像这样:

data = [
   bytearray(b'\x01'),
   bytearray(b'\x03'),
   bytearray(b'\x04'),
   bytearray(b'\x01'),
   bytearray(b'\x05'),
   bytearray(b'\x00'),
   bytearray(b'\xC0'),
   bytearray(b'\xfa'),
   bytearray(b'3')
]

这个数组是我从传感器读取的。我需要的是将 data[3]data[4] 一起使用(所以 01 05 )将其转换为整数(应该为 261) 以从传感器获取值。我很难做到。有人可以帮忙吗?

最佳答案

int 为此提供了一个替代构造函数:

>>> int.from_bytes(data[3] + data[4], 'big')
261

https://stackoverflow.com/questions/65102769/

相关文章:

xml - 如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件

git - Git 中基于内容的寻址有什么好处?

reactjs - 类型 'onClick' 上不存在属性 '{ children?: ReactN

java - `Thread.sleep` 与 Project Loom for Java 中的虚拟

git - 为什么我没有得到相同的 SHA-1?

algorithm - GPU 上的线性排序。并行处理会改变 Big-O 吗?

linux-kernel - linux cdc_ecm 驱动程序与 rndis 驱动程序

ansible - 删除文件夹文件夹内早于 x 天的文件

list - 如何将列表合并到 Elixir 中的元组列表中?

linux - 如何使用 xargs 将 ls 通过管道传输到 cat 中,以便列出文件名?