使用背景

最近在做BLE设备端开发,为调试方便需要Json消息 Ascii-Hex格式互转,所以用tkinter简单做了一个图形化的Json Ascii - hex转换工具,照例分享一下下,开发过程还是习惯python随手打开一个Tools用着方便。。。

在嵌入式开发中,设备端获取的json数据通过16进制 hex进行存储和显示的,这时候就需要Ascii hex数据格式互相转换的工具,方便以Ascii核对具体json消息。

先上图:
Json Ascii -> Hex
在这里插入图片描述

Json Hex -> Ascii
在这里插入图片描述

代码解析 - Ascii-Hex转换

Asc-Hex直接使用binascii库函数,其实不止json,所有的ascii 都可以通过这个方法互转hex。。

def Ascii_to_Hex(ascii_str):
    hex_str = binascii.hexlify(ascii_str.encode())
    return hex_str

def Hex_to_Ascii(hex_str):
    hex_str = hex_str.replace(' ', '').replace('0x', '').replace('\t', '').replace('\n', '')
    ascii_str = binascii.unhexlify(hex_str.encode())
    return ascii_str

代码解析 -Tkinter 图形界面,

界面很简单,主要就是两个Text框和三个Button,点击Button会获取输入Text框里面数据,然后调用上面函数对数据解析,再显示到Output Text里面,

这个结构也是我之前其他工具沿用的。。


root = Tk()
root.geometry('1000x600')
root.title('  Json Ascii<--> Hex Converter')
root.config(bg='#f0ffff')

#Lable
intro = Label(root,text='请在左侧输入要转换的消息,然后按相应按钮      HowieXue.blog.csdn.net',\
                       bg='#d3fbfb',\
                       fg='red',\
                       font=('华文新魏',14),\
                       width=20,\
                       height=2,\
                       relief=RIDGE)

intro.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)

#Input
inputData = Text(root, font = ('',12))
inputData.place(relx=0.1, rely=0.2, relwidth=0.3, relheight=0.6)

#Output
txt = Text(root, font = ('',12))
txt.place(relx=0.6, rely=0.2, relwidth=0.3, relheight=0.6)

#Button 
bt_json2hex = Button(root, text='Json -> Hex', command=json2hex, fg ='blue')
bt_json2hex.place(relx=0.4, rely=0.25, relwidth=0.2, relheight=0.1)

bt_hex2json = Button(root, text='Hex -> Json', command=hex2json, fg ='blue')
bt_hex2json.place(relx=0.4, rely=0.45, relwidth=0.2, relheight=0.1)

bt_clear = Button(root, text='Clear', command=clearContent, fg ='blue')
bt_clear.place(relx=0.4, rely=0.65, relwidth=0.2, relheight=0.1)

完整代码

# -*- coding: utf-8 -*-
#HowieXue
from tkinter import *
import binascii


def ascii_to_hex(ascii_str):
    hex_str = binascii.hexlify(ascii_str.encode())
    return hex_str

def hex_to_ascii(hex_str):
    hex_str = hex_str.replace(' ', '').replace('0x', '').replace('\t', '').replace('\n', '')
    ascii_str = binascii.unhexlify(hex_str.encode())
    return ascii_str

def json2hex():
     ascii_input = str(inputData.get(0.0,END))
     #remove /n
     ascii_input = ascii_input.strip("\n")
     input_len = len(ascii_input)

     hex_output = ascii_to_hex(ascii_input)
     strHex = '{0}'.format(hex_output)

     print('hex result is: '+ strHex)
     #print('input Json msg len is: '+input_len)

     strShow = ' >>Json -> Hex result:\n' + strHex
     output.insert(END, strShow)
     strShow = '\n >>Input Json msg length:\n' + '{0}\n'.format(hex(input_len))
     output.insert(END, strShow)
     #inputData.delete(0.0, END)  


def hex2json():
    hex_input = str(inputData.get(0.0, END))
    # remove /n
    hex_input = hex_input.strip("\n")

    hex_output = hex_to_ascii(hex_input)
    input_len = len(hex_output)
    strjson = '{0}'.format(hex_output)


    print('json result is: ' + strjson)

    strShow = '\n >>Hex -> Json result:\n' + strjson
    output.insert(END, strShow)
    #strShow = '\n >> Json msg length:\n' + '{0}\n'.format(hex(input_len))
    #output.insert(END, strShow)
    # inputData.delete(0.0, END)


def clearContent():
     inputData.delete(1.0, END)  
     output.delete(1.0,END)


root = Tk()
root.geometry('1000x600')
root.title('  Json Ascii<--> Hex Converter')
root.config(bg='#f0ffff')

#Lable
intro = Label(root,text='请在左侧输入要转换的消息,然后按相应按钮      HowieXue.blog.csdn.net',\
                       bg='#d3fbfb',\
                       fg='red',\
                       font=('华文新魏',14),\
                       width=20,\
                       height=2,\
                       relief=RIDGE)

intro.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)

#Input
inputData = Text(root, font = ('',12))
inputData.place(relx=0.1, rely=0.2, relwidth=0.3, relheight=0.6)

#Output
output = Text(root, font = ('',12))
output.place(relx=0.6, rely=0.2, relwidth=0.3, relheight=0.6)

#Button 
bt_json2hex = Button(root, text='Json -> Hex', command=json2hex, fg ='blue')
bt_json2hex.place(relx=0.4, rely=0.25, relwidth=0.2, relheight=0.1)

bt_hex2json = Button(root, text='Hex -> Json', command=hex2json, fg ='blue')
bt_hex2json.place(relx=0.4, rely=0.45, relwidth=0.2, relheight=0.1)

bt_clear = Button(root, text='Clear', command=clearContent, fg ='blue')
bt_clear.place(relx=0.4, rely=0.65, relwidth=0.2, relheight=0.1) 


root.mainloop()


使用过程中,如果有不同的消息类型,一般通过MsgHeader区分,可以使用CheckButton勾选,来实现对不同类型协议的解析,这和具体协议定义相关,相关代码就不在这里push了,仅举个checkbutton显示实例:
在这里插入图片描述

#checkButton
RWVar = IntVar()  # 定义RWVar和FmtVar用来存放选择行为返回值
FmtVar = IntVar()
SegmVar = IntVar()

RWVar.set(1)
FmtVar.set(1)
readwrite = Checkbutton(root, text='Read',variable=RWVar, onvalue=1, offvalue=0, command=ConvertMsgHeader)
readwrite.place(relx=0.33, rely=0.85, relwidth=0.05, relheight=0.05)

dataformat = Checkbutton(root, text='Json',variable=FmtVar, onvalue=1, offvalue=0, command=ConvertMsgHeader)
dataformat.place(relx=0.23, rely=0.85, relwidth=0.05, relheight=0.05)

segmented = Checkbutton(root, text='NoSegment',variable=SegmVar, onvalue=1, offvalue=0, command=ConvertMsgHeader)
segmented.place(relx=0.41, rely=0.85, relwidth=0.09, relheight=0.05)


博主热门文章推荐:

一篇读懂系列:

LoRa Mesh系列:

网络安全系列:

嵌入式开发系列:

AI / 机器学习系列:


在这里插入图片描述

其他好玩的python脚本

Python实现自动发送邮件 --自动抓取博客/网站中留言的邮箱并发送相应邮件
Python自动生成代码 - 通过tkinter图形化操作并生成代码框架
Python解析CSV数据 - 通过Pandas解析逻辑分析仪导出的CSV数据
Python通过Django搭建网站执行Lua脚本 (实现数据解析)

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 1 天前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐