Python 字节串转Hex字符串(一个久远的问题点总结)
- 时间: 2024.07.30 作者: Yuan
这是一个今天看来似乎有点久远的问题,但是值得被记录和澄清一下!
那是在2022年1月份参与的一个项目中遇到的问题,大概需求是利用 SHT40-AD1B-R2 芯片,读取环境温度。其实就是通过 i2c 与这个温度传感器建立通讯,然后读取温湿度信息,对于上位机的我而言,需要解决的就是将从传感器中读回的数据换算为以摄氏度为单位的十进制数值。这个当时可能困扰了我一两天,但是当时还是被我找到方法"解决"了。
大概在2022年6月份的时候,在梳理 Python 知识点的过程中,我猛然间发现似乎不用我当时那么复杂的解决方法,但我却吭哧吭哧搞了好久。就好比不知已有"打火机",自己愚蠢且努力地搞了一套"钻木取火工具"。
现在时2024年7月份,事情过去都两年了,才忽然想起要整理下这个问题。由于当时对数据对象的认识不足以及python掌握的不够清楚,当我现在再次确认当前的解决办法时,发现了一些问题。现在就来解一解这个两年前的bug。
- 数据内容与说明
"""
当时在看到读回的数据为类似 `b'fX'`、`b'f4'`、`b'd\xac\xd6\xa5\x9f\x1e'` 这样的结果时,一时间有点摸不着头脑,
于是请教了客户那边的软件工程师,客户给我做了一个示例。
有了客户的示例和指导后,我就奔着目标开干了:
将遇到的\x开头的2位16进制字符记为16进制,非\x开头的16进制的字符转为 ascii 码!
"""
# Yuan:I2C 读取温度返回的信息, 有点奇怪: b'f4' 、b'fX' 、b'f`5\x95\xA6\xE1'
# Kim:什麼型號的溫度感測元件
# Yuan:SHT40-AD1B-R2
# Kim:
b'f4' 算出來是24.866483558 °C
我覺得應該是正常的
b'f4' = 0x66, 0x34
0x6634 = 26164
((26164/65535)*175) - 45 = 24.866483558
b'f'' = 0x66, 0x27
' 這個的ascii code應該是0x27
# Yuan:是用 ascii code 转换成16进制的对吗?
# Kim:
Yes,因為那是byte array
除非看到\x
不然就都要轉ASCII
# Yuan:好的,\x开头的都是2位16进制, 对吗
# Kim:Yes
- 当时的解决方案
#!/usr/bin/env python3
"""
思路:
将 “\x+2位字符” 部分直接记录位 Hex 值,将非“\x+2位字符”的单个字符转换为 ASCII 码,
于是,我突发奇想从utf-8中找了几个希腊字母(因为他们不在ascii表中,避免了重复),先将 “\x+2位字符” 这类已知的Hex字串替换成希腊字母,
然后,再遍历替换后的字符串,遇到的非希腊字母,就将其从 ASCII 码,转换为对应的Hex形式。
最终,将两类Hex字符汇总成纯Hex字符的字符串,再统一转换为十进制数值,再进行下一步温度的计算。
"""
import re
temp_list = [
b'dW',
b'd^',
b'dB',
b'd>',
b'dP',
b'dD',
b'dS',
b'dQ',
b'dP',
b'dO',
b'dK',
b'',
b'dU',
b'df',
b'd_',
b'dd(\xa6\x99\x95',
b'd\xa4o\xa5\xc2,',
b'd\xb0\xe8\xa5\xddA',
b'd\xa3\xf8\xa5\xaa.',
b'd\xb0\xe8\xa5\xac\x88',
b'd\xb8Q\xa5\xb3\xe5',
b'd\xac\xd6\xa5\x8b\x99',
b'd\xaap\xa5\xa41',
b'd\xae\xb4\xa5\xa7b',
b'd\xa7<\xa5\x93c',
b'd\xa5^\xa5\x98\x89',
b'd\xac\xd6\xa5\x9f\x1e',
b'd\xb9`\xa5\x9d|',
b'd\xa7<\xa5\xa3\xa6',
b'd\xb1\xd9\xa5\x9a\xeb',
b'd\xb8Q\xa5\x94\xf4',
b'd\xb9`\xa5\x89\xfb',
b'd\xb8Q\xa5xH',
b'',
b'd\xb3\xbb\xa5\x91\x01',
b'd\xb0\xe8\xa5\x96\x96',
b'd\xba3\xa5\x7f\xdf',
b'd\xb2\x8a\xa5xH',
b'd\xaap\xa5s\xa2',
b'd\xabA\xa5s\xa2',
b'd\xba3\xa5\x81B',
b'd\xb8Q\xa5n\xad',
b'd\xb2\x8a\xa5yy',
b'd\xac\xd6\xa5h\x0b',
b'd\xb6N\xa5n\xad',
b'd\xabA\xa5wf',
b'd\xad\xe7\xa5l\xcf',
b'd\xbc\x95\xa5i:',
b'd\xb3\xbb\xa5];',
b'd\xb1\xd9\xa5eG',
b'd\xbb\x02\xa5s\xa2',
b'd\xac\xd6\xa5p\xf1',
b'',
b'd\xad\xe7\xa5\\\n',
b'\n',
b'd\n',
b'd\xbc\x95\xa5O\x1a',
b'd\xb1\xd9\xa5LI']
def get_temperature(_data):
if _data:
data1 = str(_data).lstrip("b").strip("'")
reg = r'\\x([0-9a-f]{2})'
val_ret = re.findall(reg, data1)
unichr = 'δλξπσω'
unidict = {}
data2 = str(data1)
if val_ret:
for i, ret in enumerate(val_ret):
unidict[unichr[i]] = '0x{}'.format(ret)
data2 = data2.replace('\\x{}'.format(ret), unichr[i])
hex_str = []
for da in data2:
if da in unidict.keys():
hex_str.append(unidict[da])
else:
hex_str.append(hex(ord(da)))
data_pool = hex_str
value1 = int(''.join(data_pool[:2]).replace('0x', ''), 16)
degc = round(((value1 / 65535) * 175) - 45, 2)
# prh = round(((value2 / 65535) * 125) - 6, 2)
# if prh > 100:
# prh = 100
# if prh < 0:
# prh = 0
# unit = chr(8451)
unit = "°C"
if degc > 40 or degc < 10:
result = 'degc: {} {} ({}, {})'.format(degc, unit, _data, val_ret)
else:
result = 'degc: {} {}'.format(degc, unit)
# print('DEG: {} {}'.format(degc, unit), ', PRH: {} {}'.format(prh, '%'))
# print("{} ==> {} ==> Hex: {} ==> DEG: {} {}".format(_data, val_ret, data_pool, degc, unit))
print(f"{_data} ==> {val_ret} ==> Hex: {data_pool} ==> DEG: {degc} {unit}")
else:
result = '-- no data --'
return result
if __name__ == '__main__':
for i, temp in enumerate(temp_list):
# print('{}: {} {}'.format(str(i).zfill(2), get_temperature(temp), temp))
get_temperature(temp)
"""
输出:
b'dW' ==> [] ==> Hex: ['0x64', '0x57'] ==> DEG: 23.59 °C
b'd^' ==> [] ==> Hex: ['0x64', '0x5e'] ==> DEG: 23.61 °C
b'dB' ==> [] ==> Hex: ['0x64', '0x42'] ==> DEG: 23.54 °C
b'd>' ==> [] ==> Hex: ['0x64', '0x3e'] ==> DEG: 23.53 °C
b'dP' ==> [] ==> Hex: ['0x64', '0x50'] ==> DEG: 23.57 °C
b'dD' ==> [] ==> Hex: ['0x64', '0x44'] ==> DEG: 23.54 °C
b'dS' ==> [] ==> Hex: ['0x64', '0x53'] ==> DEG: 23.58 °C
b'dQ' ==> [] ==> Hex: ['0x64', '0x51'] ==> DEG: 23.58 °C
b'dP' ==> [] ==> Hex: ['0x64', '0x50'] ==> DEG: 23.57 °C
b'dO' ==> [] ==> Hex: ['0x64', '0x4f'] ==> DEG: 23.57 °C
b'dK' ==> [] ==> Hex: ['0x64', '0x4b'] ==> DEG: 23.56 °C
b'dU' ==> [] ==> Hex: ['0x64', '0x55'] ==> DEG: 23.59 °C
b'df' ==> [] ==> Hex: ['0x64', '0x66'] ==> DEG: 23.63 °C
b'd_' ==> [] ==> Hex: ['0x64', '0x5f'] ==> DEG: 23.61 °C
b'dd(\xa6\x99\x95' ==> ['a6', '99', '95'] ==> Hex: ['0x64', '0x64', '0x28', '0xa6', '0x99', '0x95'] ==> DEG: 23.63 °C
b'd\xa4o\xa5\xc2,' ==> ['a4', 'a5', 'c2'] ==> Hex: ['0x64', '0xa4', '0x6f', '0xa5', '0xc2', '0x2c'] ==> DEG: 23.8 °C
b'd\xb0\xe8\xa5\xddA' ==> ['b0', 'e8', 'a5', 'dd'] ==> Hex: ['0x64', '0xb0', '0xe8', '0xa5', '0xdd', '0x41'] ==> DEG: 23.83 °C
b'd\xa3\xf8\xa5\xaa.' ==> ['a3', 'f8', 'a5', 'aa'] ==> Hex: ['0x64', '0xa3', '0xf8', '0xa5', '0xaa', '0x2e'] ==> DEG: 23.8 °C
b'd\xb0\xe8\xa5\xac\x88' ==> ['b0', 'e8', 'a5', 'ac', '88'] ==> Hex: ['0x64', '0xb0', '0xe8', '0xa5', '0xac', '0x88'] ==> DEG: 23.83 °C
b'd\xb8Q\xa5\xb3\xe5' ==> ['b8', 'a5', 'b3', 'e5'] ==> Hex: ['0x64', '0xb8', '0x51', '0xa5', '0xb3', '0xe5'] ==> DEG: 23.85 °C
b'd\xac\xd6\xa5\x8b\x99' ==> ['ac', 'd6', 'a5', '8b', '99'] ==> Hex: ['0x64', '0xac', '0xd6', '0xa5', '0x8b', '0x99'] ==> DEG: 23.82 °C
b'd\xaap\xa5\xa41' ==> ['aa', 'a5', 'a4'] ==> Hex: ['0x64', '0xaa', '0x70', '0xa5', '0xa4', '0x31'] ==> DEG: 23.81 °C
b'd\xae\xb4\xa5\xa7b' ==> ['ae', 'b4', 'a5', 'a7'] ==> Hex: ['0x64', '0xae', '0xb4', '0xa5', '0xa7', '0x62'] ==> DEG: 23.83 °C
b'd\xa7<\xa5\x93c' ==> ['a7', 'a5', '93'] ==> Hex: ['0x64', '0xa7', '0x3c', '0xa5', '0x93', '0x63'] ==> DEG: 23.81 °C
b'd\xa5^\xa5\x98\x89' ==> ['a5', 'a5', '98', '89'] ==> Hex: ['0x64', '0xa5', '0x5e', '0xa5', '0x98', '0x89'] ==> DEG: 23.8 °C
b'd\xac\xd6\xa5\x9f\x1e' ==> ['ac', 'd6', 'a5', '9f', '1e'] ==> Hex: ['0x64', '0xac', '0xd6', '0xa5', '0x9f', '0x1e'] ==> DEG: 23.82 °C
b'd\xb9`\xa5\x9d|' ==> ['b9', 'a5', '9d'] ==> Hex: ['0x64', '0xb9', '0x60', '0xa5', '0x9d', '0x7c'] ==> DEG: 23.85 °C
b'd\xa7<\xa5\xa3\xa6' ==> ['a7', 'a5', 'a3', 'a6'] ==> Hex: ['0x64', '0xa7', '0x3c', '0xa5', '0xa3', '0xa6'] ==> DEG: 23.81 °C
b'd\xb1\xd9\xa5\x9a\xeb' ==> ['b1', 'd9', 'a5', '9a', 'eb'] ==> Hex: ['0x64', '0xb1', '0xd9', '0xa5', '0x9a', '0xeb'] ==> DEG: 23.83 °C
b'd\xb8Q\xa5\x94\xf4' ==> ['b8', 'a5', '94', 'f4'] ==> Hex: ['0x64', '0xb8', '0x51', '0xa5', '0x94', '0xf4'] ==> DEG: 23.85 °C
b'd\xb9`\xa5\x89\xfb' ==> ['b9', 'a5', '89', 'fb'] ==> Hex: ['0x64', '0xb9', '0x60', '0xa5', '0x89', '0xfb'] ==> DEG: 23.85 °C
b'd\xb8Q\xa5xH' ==> ['b8', 'a5'] ==> Hex: ['0x64', '0xb8', '0x51', '0xa5', '0x78', '0x48'] ==> DEG: 23.85 °C
b'd\xb3\xbb\xa5\x91\x01' ==> ['b3', 'bb', 'a5', '91', '01'] ==> Hex: ['0x64', '0xb3', '0xbb', '0xa5', '0x91', '0x01'] ==> DEG: 23.84 °C
b'd\xb0\xe8\xa5\x96\x96' ==> ['b0', 'e8', 'a5', '96', '96'] ==> Hex: ['0x64', '0xb0', '0xe8', '0xa5', '0x96', '0x96'] ==> DEG: 23.83 °C
b'd\xba3\xa5\x7f\xdf' ==> ['ba', 'a5', '7f', 'df'] ==> Hex: ['0x64', '0xba', '0x33', '0xa5', '0x7f', '0xdf'] ==> DEG: 23.86 °C
b'd\xb2\x8a\xa5xH' ==> ['b2', '8a', 'a5'] ==> Hex: ['0x64', '0xb2', '0x8a', '0xa5', '0x78', '0x48'] ==> DEG: 23.84 °C
b'd\xaap\xa5s\xa2' ==> ['aa', 'a5', 'a2'] ==> Hex: ['0x64', '0xaa', '0x70', '0xa5', '0x73', '0xa2'] ==> DEG: 23.81 °C
b'd\xabA\xa5s\xa2' ==> ['ab', 'a5', 'a2'] ==> Hex: ['0x64', '0xab', '0x41', '0xa5', '0x73', '0xa2'] ==> DEG: 23.82 °C
b'd\xba3\xa5\x81B' ==> ['ba', 'a5', '81'] ==> Hex: ['0x64', '0xba', '0x33', '0xa5', '0x81', '0x42'] ==> DEG: 23.86 °C
b'd\xb8Q\xa5n\xad' ==> ['b8', 'a5', 'ad'] ==> Hex: ['0x64', '0xb8', '0x51', '0xa5', '0x6e', '0xad'] ==> DEG: 23.85 °C
b'd\xb2\x8a\xa5yy' ==> ['b2', '8a', 'a5'] ==> Hex: ['0x64', '0xb2', '0x8a', '0xa5', '0x79', '0x79'] ==> DEG: 23.84 °C
b'd\xac\xd6\xa5h\x0b' ==> ['ac', 'd6', 'a5', '0b'] ==> Hex: ['0x64', '0xac', '0xd6', '0xa5', '0x68', '0x0b'] ==> DEG: 23.82 °C
b'd\xb6N\xa5n\xad' ==> ['b6', 'a5', 'ad'] ==> Hex: ['0x64', '0xb6', '0x4e', '0xa5', '0x6e', '0xad'] ==> DEG: 23.85 °C
b'd\xabA\xa5wf' ==> ['ab', 'a5'] ==> Hex: ['0x64', '0xab', '0x41', '0xa5', '0x77', '0x66'] ==> DEG: 23.82 °C
b'd\xad\xe7\xa5l\xcf' ==> ['ad', 'e7', 'a5', 'cf'] ==> Hex: ['0x64', '0xad', '0xe7', '0xa5', '0x6c', '0xcf'] ==> DEG: 23.82 °C
b'd\xbc\x95\xa5i:' ==> ['bc', '95', 'a5'] ==> Hex: ['0x64', '0xbc', '0x95', '0xa5', '0x69', '0x3a'] ==> DEG: 23.86 °C
b'd\xb3\xbb\xa5];' ==> ['b3', 'bb', 'a5'] ==> Hex: ['0x64', '0xb3', '0xbb', '0xa5', '0x5d', '0x3b'] ==> DEG: 23.84 °C
b'd\xb1\xd9\xa5eG' ==> ['b1', 'd9', 'a5'] ==> Hex: ['0x64', '0xb1', '0xd9', '0xa5', '0x65', '0x47'] ==> DEG: 23.83 °C
b'd\xbb\x02\xa5s\xa2' ==> ['bb', '02', 'a5', 'a2'] ==> Hex: ['0x64', '0xbb', '0x02', '0xa5', '0x73', '0xa2'] ==> DEG: 23.86 °C
b'd\xac\xd6\xa5p\xf1' ==> ['ac', 'd6', 'a5', 'f1'] ==> Hex: ['0x64', '0xac', '0xd6', '0xa5', '0x70', '0xf1'] ==> DEG: 23.82 °C
b'd\xad\xe7\xa5\\\n' ==> ['ad', 'e7', 'a5'] ==> Hex: ['0x64', '0xad', '0xe7', '0xa5', '0x5c', '0x5c', '0x5c', '0x6e'] ==> DEG: 23.82 °C
b'\n' ==> [] ==> Hex: ['0x5c', '0x6e'] ==> DEG: 18.19 °C
b'd\n' ==> [] ==> Hex: ['0x64', '0x5c', '0x6e'] ==> DEG: 23.61 °C
b'd\xbc\x95\xa5O\x1a' ==> ['bc', '95', 'a5', '1a'] ==> Hex: ['0x64', '0xbc', '0x95', '0xa5', '0x4f', '0x1a'] ==> DEG: 23.86 °C
b'd\xb1\xd9\xa5LI' ==> ['b1', 'd9', 'a5'] ==> Hex: ['0x64', '0xb1', '0xd9', '0xa5', '0x4c', '0x49'] ==> DEG: 23.83 °C
"""
- 半年后发现的办法
# 原始字节数据
byte_data = b'd\xad\xe7\xa5\\\n'
# 转换为十六进制字符串
hex_data = byte_data.hex()
# 打印结果
print(hex_data)
"""
输出:
'64ade7a55c0a'
"""
# 如果需要将十六进制字符串格式化成每个字节两位并用空格分隔的形式,可以使用以下代码
# 原始字节数据
byte_data = b'd\xad\xe7\xa5\\\n'
# 转换为十六进制字符串并分隔每个字节
hex_data = byte_data.hex()
formatted_hex = ' '.join([hex_data[i:i+2] for i in range(0, len(hex_data), 2)])
# 打印结果
print(formatted_hex)