簡體   English   中英

在python 2.7中將4字節塊轉換為相應的int

[英]Converting a 4 byte block to the corresponding int in python 2.7

我從arduino傳遞了一個字節序列,這些序列是傳感器讀數,我需要這些字節的整數值。 我的字節塊大小為sizeof(int)* 18(因為我有18個傳感器),其中sizeof(int)為4。

有人可以告訴我如何在python中找到4個字節的int值嗎? 我需要為每個傳感器讀數執行此操作。

您可以使用struct.unpack

>>> import struct
>>> # assume the `sensor_values` is come from sensors.
>>> sensor_values = struct.pack('!18i', *range(100, 1900, 100))
>>> print struct.unpack('!18i', sensor_values)
(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,
 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800)

! 表示數據打包在網絡(大端)中。 如果數據以little-endian打包,請使用<

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM