背景:
- 由于用到系统中api,获取到的数据类型为ctypes.c_uint8,在C语言中可直接使用memcpy将uint8数组拷贝到float类型变量中,python不能操作地址,因此需要进行数据转化。
转化方式:
import struct
#ctypes.c_uint8类型数组,用于接收数据
value = (ctypes.c_uint8 * 2000)(0x00)
#unpack return type is tuple
temp = struct.unpack('<f', struct.pack('4B', value[0], value[1], value[2], value[3]))
print ("temperature f:%f"%temp[0])
说明:
- struct.unpack 返回类型为tuple