在python中uchar到ushort

2024-09-29 23:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我的标题可能看起来很奇怪,因为Python中没有uchar和ushort这样的东西,所以让我解释一下: 我从大容量传输中得到一个8位数据的列表,但我需要16位的数据。因此,我这样做:

frame = dev.read(0x82, packetLength, interface, timeout) # bulk transfer
if len(frame) == packetLength
    for i in range(0, packetLength, 2):
        newFrame.append(frame[i+1]*256 + frame[i])

所以是的,它是有效的,但它是非常缓慢的,我需要运行它在树莓皮。。。在

你们中有谁知道做同样事情更好的方法吗?在


Tags: 数据dev标题列表readiftimeoutbulk
1条回答
网友
1楼 · 发布于 2024-09-29 23:24:14

使用struct,解决方案是:

import struct

frame = dev.read( ... )

fmt = "<%dH" % (len(frame) / 2)
newFrame = struct.unpack(fmt, frame)

fmt字符串说明:

  • <-数据采用little-endian格式
  • %d-数组的大小(它修改以下格式说明符)
  • H-格式说明符将数据解释为ushorts

相关问题 更多 >

    热门问题