[英]Python: How Can I Convert Hexadecimal Characters to CType Data Types (e.g., uint32_t, uint16_t, etc.)?
Does anyone know a way to convert a string of hexadecimal characters into particular ctype variables? 有没有人知道将一串十六进制字符转换为特定ctype变量的方法?
To elaborate, I need create a function which takes in a hexadecimal list of characters and a string representing the datatype and produces a value according to the datatype specified. 为了详细说明,我需要创建一个函数,它接受一个十六进制字符列表和一个表示数据类型的字符串,并根据指定的数据类型生成一个值。
I am parsing MAVlink fields whose values take on data types of: uint64_t, uint32_t, int32_t, uint16_t, int16_t, uint8_t, int8_t, float, char 我正在解析MAVlink字段,其值取数据类型为:uint64_t,uint32_t,int32_t,uint16_t,int16_t,uint8_t,int8_t,float,char
Additionally, the values of the fields can be arrays of the above types. 另外,字段的值可以是上述类型的数组。
This is what my function looks like now: 这就是我现在的功能:
def getValue(payload, ftype):
# Check if signed or unsigned
if ftype.find("uint") != -1: signed = False
# Check size of data type
if ftype.find('int8_t') != -1: stride = 1
elif ftype.find('int16_t') != -1: stride = 2
elif ftype.find('int32_t') != -1: stride = 4
elif ftype.find('float') != -1: stride = 4
else: stride = 1 (Assume characters)
chars = []
while(stride > 0):
# Pop two characters to get one byte
chars.append(payload.pop(0))
chars.append(payload.pop(0))
stride -= 1
# Convert selected characters to value of specified data type
return convertToValueAsString(chars, ftype) # I don't know how to do this
I've take a look at ctypes and using ctypes.uint*_t.value(a). 我看一下ctypes并使用ctypes.uint * _t.value(a)。 However, I'm not sure how to use this function when the data type is a string and not a known value. 但是,当数据类型是字符串而不是已知值时,我不确定如何使用此函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.