項目中使用到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()
此篇部落格僅做記錄用。