项目中使用到Python脚本进行边缘计算,其中有一个需求在测试的时候需要使用到十六进制的数据,在查阅资料之后决定使用bytearray的fromhex函数,示例代码如下:
def dealWithArray(bytes):
if len(bytes) < 40:
return ''
else:
mode = bytes[10]
if mode == 0:
return 'RESET'
elif mode == 1:
return 'STOP'
elif mode == 2:
return 'BREAK'
elif mode == 3:
return 'RUN'
else:
return ''
return ''
def main():
bytes = bytearray.fromhex("00 00 01 00 7b 00 08 00 00 00"
"00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 0F")
print(dealWithArray(bytes))
if __name__ == '__main__':
main()
此篇博客仅做记录用。