最近遇到的问题
网络传输socket 需要字节流byte,而UI对话框输入hexstr串,在晚上搜索了一下,做如下记录
- ByteToHex的转换
def ByteToHex( bins ): """ Convert a byte string to it's hex string representation e.g. for output. """ return ''.join( [ "%02X" % x for x in bins ] ).strip()
- HexToByte的转换
def HexToByte( hexStr ): """ Convert a string hex byte values into a byte string. The Hex Byte values may or may not be space separated. """ return bytes.fromhex(hexStr)
测试
>>> hexStr2 = "FF FF FF 5F 81 21 07 0C 00 00 FF FF FF FF 5F 81 29 01 0B" >>> hexStr2 'FF FF FF 5F 81 21 07 0C 00 00 FF FF FF FF 5F 81 29 01 0B'
bytes.fromhex(hexStr2) b'\xff\xff\xff_\x81!\x07\x0c\x00\x00\xff\xff\xff\xff_\x81)\x01\x0b'