首页 > 编程语言 >Python 字节串转Hex字符串(一个久远的问题点总结)

Python 字节串转Hex字符串(一个久远的问题点总结)

时间:2024-07-30 18:08:04浏览次数:13  
标签:Python Hex xa5 a5 串转 0x64 0xa5 DEG

  • 时间: 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)

标签:Python,Hex,xa5,a5,串转,0x64,0xa5,DEG
From: https://www.cnblogs.com/jswl/p/18332870

相关文章

  • [附开题]flask框架的汽车零件维修管理信息平台t6rr1(python+源码)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着汽车工业的快速发展和汽车保有量的持续增长,汽车零件维修管理已成为汽车后市场的重要组成部分。传统的手工记录和管理方式已难以满足现......
  • [附开题]flask框架的汽车售后管理系统2888o(python+源码)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着汽车产业的快速发展和消费者购车需求的日益增长,汽车售后服务已成为车企和经销商提升客户满意度、增强品牌忠诚度的重要环节。然而,传统......
  • [附开题]flask框架的社区服务管理系统0f6i9(python+源码)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着城市化进程的深入和居民生活水平的提高,社区服务需求日益多样化与复杂化。传统的社区服务模式已难以满足居民对于高效、便捷、个性化服......
  • [附开题]flask框架的社区服务系统ff00q(python+源码)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着社会的快速发展和居民生活水平的提高,社区作为城市的基本单元,其服务功能日益丰富和多样化。然而,传统社区服务方式在应对日益增长的居民......
  • Python基础知识笔记---保留字
    保留字,也称关键字,是指被编程语言内部定义并保留使用的标识符。一、保留字概览  二、保留字用途 1.`False`:表示布尔值假。2.`None`:表示空值或无值。3.`True`:表示布尔值真。4.`not`:布尔逻辑操作符,对条件表达式取反。5.`or`:布尔逻辑操作符,用于连接两个条件表达式......
  • 用Python写一个植物大战僵尸
    导语:哈喽,哈喽~植物大战僵尸的人气可谓是经久不衰,晃着脑袋生产阳光的向日葵,突突突⚾⚾⚾吐着子弹的豌豆射手!​行动迟缓种类丰富的僵尸……印象最深的是“僵尸吃掉了你的脑子!”还有疯狂的戴夫,无一不唤醒着我们的童年记忆​。下面用python还原你的记忆中的童年!功能实现如下:......
  • 基于Python实现的深度学习技术在水文水质领域应用
    当前,深度学习作为人工智能的热门技术发展迅速,以其强大的非线性和不确定性处理能力在图像识别、语音识别、自然语言处理等领域取得了显著的成效。它是一种端到端的处理方法,在训练算法的指导下,深层神经网络自发地从原始数据中进行抽象,提炼关键特征,对结果做出预报,中间过程不需要人......
  • 为什么 string.maketrans 在 Python 3.1 中不起作用?
    我是Python新手。怎么了这个在Python3.1中不起作用?fromstringimportmaketrans#Requiredtocallmaketransfunction.intab="aeiou"outtab="12345"trantab=maketrans(intab,outtab)str="thisisstringexample....wow!......
  • Python - Creating alternative initializers using class Methods
    Classmethodsallowustodefinealternativeinitializers(alsoknownasfactorymethods)inaclass.Thesemethodshelpuscreateinstanceobjectsfromdifferenttypesofinputdata.Letusunderstandthiswiththehelpofanexample.Again,wetakethe......
  • 如何让 Python 请求信任自签名 SSL 证书?
    importrequestsdata={'foo':'bar'}url='https://foo.com/bar'r=requests.post(url,data=data)如果URL使用自签名证书,则会失败requests.exceptions.SSLError:[Errno1]_ssl.c:507:error:14090086:SSLroutines:SSL3_GET_SERVER_CERTIF......