ASCII转换小工具

ASCII转换小工具

参考来源:
非常感谢这位网友,我从2017年用到2023年,一共用了近7年,一直收藏着使用。
请添加图片描述

意义:
在嵌入式领域,对字符的解析转换太常用了,基本每天都在做着ASCII和16进制互转操作。如网络端传来的16进制看不出来表示什么,车辆UDS诊断查询的VIN是16进制对应的ASCII是什么,有时候遇到表现形式是16进制但是不加前缀"0x"或者是"0X"等 不方便在线转换,就需要借助工具转换了,网上其实有在线的转换功能,但是一旦遇到分隔符或者转换的数据量比较大的时候就无能为力了。

做一款定制化的ASCII小工具还是很有实用价值的,目前实现的这个小工具,应该包括了我工作7年所遇到的80%问题,为什么不说或者不做到100%呢,因为ASCII和16进制之间的转换,样式太多了,这个工具是我根据我目前遇到的所有情况做的总结,并根据所遇到的使用场景做的归类,对这些问题进行解决,以后就常用我自己写的工具来做转换了,以后基本不使用网友那个工具了,在使用中测试这个工具,相信在长时间的使用中发现问题,解决问题,这个工具的实用价值就越来越高。

使用pyqt5,实现起来很容易,pyqt能快速构建GUI,python语法简单灵活,以前都是C语言写一些测试小程序,现在学习了python,的确在快速开发这块有先天绝对优势。

实现步骤:
1.使用Qt Designer 快速构建GUI界面。
2.根据GUI界面,实现对功能 功能设置 分隔符信息的获取,对输入数据的逻辑处理。

介绍依赖环境:
1.python
使用3.10.2
2.pyqt5
使用PyQt5 5.15.9(只要python和pyqt5能相互匹配就行)

1.使用Qt Designer 快速构建GUI界面。

在这里插入图片描述

1.1 untitled.ui文件源码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1010</width>
    <height>763</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2">
   <item>
    <widget class="QGroupBox" name="groupBox">
     <property name="title">
      <string>1.功能选择</string>
     </property>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QRadioButton" name="radioButton_hex_to_asc">
        <property name="text">
         <string>16进制转字符串</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_asc_to_hex">
        <property name="text">
         <string>字符串转16进制</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_only_format">
        <property name="text">
         <string>不转换只格式化</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_show_asc_table">
        <property name="text">
         <string>显示ascii对照表</string>
        </property>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer_3">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <widget class="QGroupBox" name="groupBox_6">
     <property name="title">
      <string>2.输出设置</string>
     </property>
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QCheckBox" name="checkBox_delete_line_breaks">
        <property name="text">
         <string>删换行</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_display_prefix">
        <property name="text">
         <string>显示0x</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_not_display_prefix">
        <property name="text">
         <string>不显示0x</string>
        </property>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer_5">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>593</width>
          <height>17</height>
         </size>
        </property>
       </spacer>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <widget class="QGroupBox" name="groupBox_3">
     <property name="title">
      <string>3.分隔符设置</string>
     </property>
     <layout class="QHBoxLayout" name="horizontalLayout_3">
      <item>
       <widget class="QCheckBox" name="checkBox_add_space">
        <property name="text">
         <string>加空格</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_delete_space">
        <property name="text">
         <string>删除空格</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_add_comma">
        <property name="text">
         <string>加,</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_delete_comma">
        <property name="text">
         <string>删除,</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_add_period">
        <property name="text">
         <string>加.</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QCheckBox" name="checkBox_delete_period">
        <property name="text">
         <string>删除.</string>
        </property>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer_4">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>593</width>
          <height>17</height>
         </size>
        </property>
       </spacer>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <widget class="QGroupBox" name="groupBox_4">
     <property name="title">
      <string>4.输入数据框</string>
     </property>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QTextEdit" name="textEdit_input"/>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <widget class="QGroupBox" name="groupBox_5">
     <property name="title">
      <string>5.输出数据框</string>
     </property>
     <layout class="QHBoxLayout" name="horizontalLayout_5">
      <item>
       <widget class="QTextEdit" name="textEdit_output"/>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <widget class="QGroupBox" name="groupBox_12">
     <property name="title">
      <string>6.结果选择</string>
     </property>
     <layout class="QHBoxLayout" name="horizontalLayout_4">
      <item>
       <widget class="QLabel" name="label_version">
        <property name="text">
         <string>XL</string>
        </property>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer_2">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>675</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton_clean">
        <property name="text">
         <string>清空</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton_convert">
        <property name="text">
         <string>转换</string>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

1.2 ui生成对应py文件Ui_untitled.py源码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'e:\xiaoliang\pyqt\Ascii_tool\untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1010, 763)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setObjectName("groupBox")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.groupBox)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.radioButton_hex_to_asc = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton_hex_to_asc.setObjectName("radioButton_hex_to_asc")
        self.horizontalLayout.addWidget(self.radioButton_hex_to_asc)
        self.radioButton_asc_to_hex = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton_asc_to_hex.setObjectName("radioButton_asc_to_hex")
        self.horizontalLayout.addWidget(self.radioButton_asc_to_hex)
        self.radioButton_only_format = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton_only_format.setObjectName("radioButton_only_format")
        self.horizontalLayout.addWidget(self.radioButton_only_format)
        self.radioButton_show_asc_table = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton_show_asc_table.setObjectName("radioButton_show_asc_table")
        self.horizontalLayout.addWidget(self.radioButton_show_asc_table)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.verticalLayout_2.addWidget(self.groupBox)
        self.groupBox_6 = QtWidgets.QGroupBox(Form)
        self.groupBox_6.setObjectName("groupBox_6")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox_6)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.checkBox_delete_line_breaks = QtWidgets.QCheckBox(self.groupBox_6)
        self.checkBox_delete_line_breaks.setObjectName("checkBox_delete_line_breaks")
        self.horizontalLayout_2.addWidget(self.checkBox_delete_line_breaks)
        self.checkBox_display_prefix = QtWidgets.QCheckBox(self.groupBox_6)
        self.checkBox_display_prefix.setObjectName("checkBox_display_prefix")
        self.horizontalLayout_2.addWidget(self.checkBox_display_prefix)
        self.checkBox_not_display_prefix = QtWidgets.QCheckBox(self.groupBox_6)
        self.checkBox_not_display_prefix.setObjectName("checkBox_not_display_prefix")
        self.horizontalLayout_2.addWidget(self.checkBox_not_display_prefix)
        spacerItem1 = QtWidgets.QSpacerItem(593, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem1)
        self.verticalLayout_2.addWidget(self.groupBox_6)
        self.groupBox_3 = QtWidgets.QGroupBox(Form)
        self.groupBox_3.setObjectName("groupBox_3")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.groupBox_3)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.checkBox_add_space = QtWidgets.QCheckBox(self.groupBox_3)
        self.checkBox_add_space.setObjectName("checkBox_add_space")
        self.horizontalLayout_3.addWidget(self.checkBox_add_space)
        self.checkBox_delete_space = QtWidgets.QCheckBox(self.groupBox_3)
        self.checkBox_delete_space.setObjectName("checkBox_delete_space")
        self.horizontalLayout_3.addWidget(self.checkBox_delete_space)
        self.checkBox_add_comma = QtWidgets.QCheckBox(self.groupBox_3)
        self.checkBox_add_comma.setObjectName("checkBox_add_comma")
        self.horizontalLayout_3.addWidget(self.checkBox_add_comma)
        self.checkBox_delete_comma = QtWidgets.QCheckBox(self.groupBox_3)
        self.checkBox_delete_comma.setObjectName("checkBox_delete_comma")
        self.horizontalLayout_3.addWidget(self.checkBox_delete_comma)
        self.checkBox_add_period = QtWidgets.QCheckBox(self.groupBox_3)
        self.checkBox_add_period.setObjectName("checkBox_add_period")
        self.horizontalLayout_3.addWidget(self.checkBox_add_period)
        self.checkBox_delete_period = QtWidgets.QCheckBox(self.groupBox_3)
        self.checkBox_delete_period.setObjectName("checkBox_delete_period")
        self.horizontalLayout_3.addWidget(self.checkBox_delete_period)
        spacerItem2 = QtWidgets.QSpacerItem(593, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem2)
        self.verticalLayout_2.addWidget(self.groupBox_3)
        self.groupBox_4 = QtWidgets.QGroupBox(Form)
        self.groupBox_4.setObjectName("groupBox_4")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox_4)
        self.verticalLayout.setObjectName("verticalLayout")
        self.textEdit_input = QtWidgets.QTextEdit(self.groupBox_4)
        self.textEdit_input.setObjectName("textEdit_input")
        self.verticalLayout.addWidget(self.textEdit_input)
        self.verticalLayout_2.addWidget(self.groupBox_4)
        self.groupBox_5 = QtWidgets.QGroupBox(Form)
        self.groupBox_5.setObjectName("groupBox_5")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.groupBox_5)
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.textEdit_output = QtWidgets.QTextEdit(self.groupBox_5)
        self.textEdit_output.setObjectName("textEdit_output")
        self.horizontalLayout_5.addWidget(self.textEdit_output)
        self.verticalLayout_2.addWidget(self.groupBox_5)
        self.groupBox_12 = QtWidgets.QGroupBox(Form)
        self.groupBox_12.setObjectName("groupBox_12")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.groupBox_12)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.label_version = QtWidgets.QLabel(self.groupBox_12)
        self.label_version.setObjectName("label_version")
        self.horizontalLayout_4.addWidget(self.label_version)
        spacerItem3 = QtWidgets.QSpacerItem(675, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_4.addItem(spacerItem3)
        self.pushButton_clean = QtWidgets.QPushButton(self.groupBox_12)
        self.pushButton_clean.setObjectName("pushButton_clean")
        self.horizontalLayout_4.addWidget(self.pushButton_clean)
        self.pushButton_convert = QtWidgets.QPushButton(self.groupBox_12)
        self.pushButton_convert.setObjectName("pushButton_convert")
        self.horizontalLayout_4.addWidget(self.pushButton_convert)
        self.verticalLayout_2.addWidget(self.groupBox_12)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.groupBox.setTitle(_translate("Form", "1.功能选择"))
        self.radioButton_hex_to_asc.setText(_translate("Form", "16进制转字符串"))
        self.radioButton_asc_to_hex.setText(_translate("Form", "字符串转16进制"))
        self.radioButton_only_format.setText(_translate("Form", "不转换只格式化"))
        self.radioButton_show_asc_table.setText(_translate("Form", "显示ascii对照表"))
        self.groupBox_6.setTitle(_translate("Form", "2.输出设置"))
        self.checkBox_delete_line_breaks.setText(_translate("Form", "删换行"))
        self.checkBox_display_prefix.setText(_translate("Form", "显示0x"))
        self.checkBox_not_display_prefix.setText(_translate("Form", "不显示0x"))
        self.groupBox_3.setTitle(_translate("Form", "3.分隔符设置"))
        self.checkBox_add_space.setText(_translate("Form", "加空格"))
        self.checkBox_delete_space.setText(_translate("Form", "删除空格"))
        self.checkBox_add_comma.setText(_translate("Form", "加,"))
        self.checkBox_delete_comma.setText(_translate("Form", "删除,"))
        self.checkBox_add_period.setText(_translate("Form", "加."))
        self.checkBox_delete_period.setText(_translate("Form", "删除."))
        self.groupBox_4.setTitle(_translate("Form", "4.输入数据框"))
        self.groupBox_5.setTitle(_translate("Form", "5.输出数据框"))
        self.groupBox_12.setTitle(_translate("Form", "6.结果选择"))
        self.label_version.setText(_translate("Form", "XL"))
        self.pushButton_clean.setText(_translate("Form", "清空"))
        self.pushButton_convert.setText(_translate("Form", "转换"))

2. 根据GUI界面,实现处理逻辑。ascii_tool.py源码

import sys

from Ui_untitled import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

#第一次编写,构建初步功能。
ASCII_VARSION = "XL_20231225_V1.0"

HEX_CHAR_SET = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f", "A","B","C","D","E","F"]

ASCII_OR_NUM_CHAR_SET = ["0","1","2","3","4","5","6","7","8","9",\
                         "A","B","C","D","E","F","G","H","I","J",\
                         "K","L","M","N","O","P","Q","R","S","T",\
                         "U","V","W","X","Y","Z",\
                         "a","b","c","d","e","f","g","h","i","j",\
                         "k","l","m","n","o","p","q","r","s","t",\
                         "u","v","w","x","y","z"]

#目前设置这三个可识别的分隔符
SEPARATOR_CHAR_LIST = [" ", ",", "."]


class Main_process(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Main_process, self).__init__(parent)
        #窗口初始化
        self.setupUi(self)

        #设置窗口背景
        #palette=QPalette()
        #palette.setBrush(QPalette.Background, QBrush(QPixmap("./background.jpeg")))
        #self.setPalette(palette)

        #给窗口一个默认的大小
        self.resize(1200, 900)

        #把窗口移动到屏幕的中心位置,这里中心向上移动了20像素点
        screen = QDesktopWidget().screenGeometry()  
        size = self.geometry()  
        self.move(int((screen.width() - size.width()) / 2),  int((screen.height() - size.height()) / 2) -20)  

        #设置标题和对应的图标
        self.setWindowTitle("ascii 转换小工具")
        self.setWindowIcon(QIcon('./xiangrikui.jpg'))

        #初始化
        self.init()

        #1.功能设置
        self.func_selec_processing_value = "hex_to_asc"
        
        #2.输出设置
        self.output_set_dict = {"delete_line_breaks": False,       #删除换行
                                "display_prefix": False,           #显示前缀0x
                                "not_display_prefix": False,       #不显示前缀0x
                                }
        #3.分隔符设置
        self.delimiter_set_dict = {"add_space": False,             #添加空格
                                   "delete_space": False,          #删除空格
                                   "add_comma": False,             #添加逗号
                                   "delete_comma": False,          #删除逗号
                                   "add_period": False,            #添加句号
                                   "delete_period": False,         #删除句号
                                }
    def init(self):
        #1.功能选择
        self.radioButton_hex_to_asc.setChecked(True)
        self.radioButton_hex_to_asc.toggled.connect(self.function_selection_processing)
        self.radioButton_asc_to_hex.setChecked(False)
        self.radioButton_asc_to_hex.toggled.connect(self.function_selection_processing)
        self.radioButton_only_format.setChecked(False)
        self.radioButton_only_format.toggled.connect(self.function_selection_processing)
        self.radioButton_show_asc_table.setChecked(False)
        self.radioButton_show_asc_table.toggled.connect(self.function_selection_processing)

        #2.输出设置
        self.checkBox_delete_line_breaks.setChecked(False)
        self.checkBox_delete_line_breaks.stateChanged.connect(self.output_set_processing)
        self.checkBox_display_prefix.setChecked(False)
        self.checkBox_display_prefix.stateChanged.connect(self.output_set_processing)
        self.checkBox_not_display_prefix.setChecked(False)
        self.checkBox_not_display_prefix.stateChanged.connect(self.output_set_processing)

        #3.分隔符处理
        self.checkBox_add_space.setChecked(False)
        self.checkBox_add_space.stateChanged.connect(self.delimiter_set_processing)
        self.checkBox_delete_space.setChecked(False)
        self.checkBox_delete_space.stateChanged.connect(self.delimiter_set_processing)
        self.checkBox_add_comma.setChecked(False)
        self.checkBox_add_comma.stateChanged.connect(self.delimiter_set_processing)
        self.checkBox_delete_comma.setChecked(False)
        self.checkBox_delete_comma.stateChanged.connect(self.delimiter_set_processing)
        self.checkBox_add_period.setChecked(False)
        self.checkBox_add_period.stateChanged.connect(self.delimiter_set_processing)
        self.checkBox_delete_period.setChecked(False)
        self.checkBox_delete_period.stateChanged.connect(self.delimiter_set_processing)

        #设置版本显示
        self.label_version.setText(ASCII_VARSION)

        #设置清除 转换按钮
        self.pushButton_clean.clicked.connect(self.clear_handle)
        self.pushButton_convert.clicked.connect(self.translate)
        
    def clear_handle(self) :
        self.textEdit_input.clear()
        self.textEdit_output.clear()
        pass

    #1.功能选择
    def  function_selection_processing(self) :
        if self.radioButton_hex_to_asc.isChecked() :
            self.func_selec_processing_value = "hex_to_asc"
        if self.radioButton_asc_to_hex.isChecked() :
            self.func_selec_processing_value = "asc_to_hex"
        if self.radioButton_only_format.isChecked() :
            self.func_selec_processing_value = "only_format"
        if self.radioButton_show_asc_table.isChecked() :
            self.func_selec_processing_value = "show_asc_table"
        pass

    #2.输出设置
    def  output_set_processing(self) :
        if True == self.checkBox_delete_line_breaks.isChecked() :
            self.output_set_dict["delete_line_breaks"] = True
        else :
            self.output_set_dict["delete_line_breaks"] = False

        if True == self.checkBox_display_prefix.isChecked() :
            self.output_set_dict["display_prefix"] = True
        else :
            self.output_set_dict["display_prefix"] = False

        if True == self.checkBox_not_display_prefix.isChecked() : 
            self.output_set_dict["not_display_prefix"] = True
        else :
            self.output_set_dict["not_display_prefix"] = False

    #3.分隔符设置
    def  delimiter_set_processing(self) :
        if True == self.checkBox_add_space.isChecked() :
            self.delimiter_set_dict["add_space"] = True
        else :
            self.delimiter_set_dict["add_space"] = False
        if True == self.checkBox_delete_space.isChecked() :
            self.delimiter_set_dict["delete_space"] = True
        else :
            self.delimiter_set_dict["delete_space"] = False

        if True == self.checkBox_add_comma.isChecked() :
            self.delimiter_set_dict["add_comma"] = True
        else :
            self.delimiter_set_dict["add_comma"] = False
        if True == self.checkBox_delete_comma.isChecked() :
            self.delimiter_set_dict["delete_comma"] = True
        else :
            self.delimiter_set_dict["delete_comma"] = False

        if True == self.checkBox_add_period.isChecked() : 
            self.delimiter_set_dict["add_period"] = True
        else :
            self.delimiter_set_dict["add_period"] = False
        if True == self.checkBox_delete_period.isChecked() : 
            self.delimiter_set_dict["delete_period"] = True
        else :
            self.delimiter_set_dict["delete_period"] = False

    def translate(self):
        output_list = []
        input_str = self.textEdit_input.toPlainText()
        
        #16--->ascii
        if self.func_selec_processing_value == "hex_to_asc" :
            self.hex_to_ascii_fun(input_str, output_list, self.output_set_dict, self.delimiter_set_dict)
        #ascii--->16
        if self.func_selec_processing_value == "asc_to_hex" :
            self.ascii_to_hex_fun(input_str, output_list, self.output_set_dict, self.delimiter_set_dict)
        #only format
        if self.func_selec_processing_value == "only_format" :
           self.only_format_fun(input_str, output_list, self.output_set_dict, self.delimiter_set_dict)
        #show ascii table
        if self.func_selec_processing_value == "show_asc_table" :
            self.show_ascii_table_fun(input_str, output_list)
    
        output_str = ""
        for tmp in output_list :
            output_str += tmp
        self.textEdit_output.setPlainText(output_str)

        #16--->ascii
        #测试数据
        # "0x61"
        # "0x610x62"
        # "0x61 0x62"
        # "0x61,0x62"
        # "0x61,0x62\r\n0x63,0x64"

        # "61"
        # "6162"
        # "61 62"
        # "61,62"
        # "61,62\r\n63,64"
    def hex_to_ascii_fun(self, input_str, output_list, output_set_dict, delimiter_set_dict) :
        #for test
        #print("0.input_str:", input_str)
        #for i in input_str:
        #    print(" ",ord(i) ,end="")
        #print(" ")

        #确认输入的字符串长度
        input_len = len(input_str)
        if 2 > input_len :
            QMessageBox.critical(self, "警告", "输入格式不对,应该是66或者0x66", QMessageBox.Cancel, QMessageBox.Cancel)
            print("input_str is null")
            return
        #for test
        #print("1.input_len:", input_len)

        tmp_value = ""
        j = 0
        while  j < input_len-1 :    #因为16进制,在字符串里面是2个字符,所以最后一个字符可以不处理
            if (input_str[j] in ["0"]) and (input_str[j+1] in ["x", "X"]) : 
                j += 2
                continue

            if (input_str[j] in HEX_CHAR_SET) and (input_str[j+1] in HEX_CHAR_SET) :
                tmp_value = input_str[j] + input_str[j+1]
                decimal_num = int(tmp_value, 16)  # 将16进制字符串转换为10进制数字
                ascii_char = chr(decimal_num)     # 将十进制数字转换为ASCII字符
                
                output_list.append(str(ascii_char))
                j += 2
                #增加空格
                if True == delimiter_set_dict["add_space"] : 
                    output_list.append(" ")
                #增加逗号
                if True == delimiter_set_dict["add_comma"] : 
                    output_list.append(",")
                #增加句号
                if True == delimiter_set_dict["add_period"] : 
                    output_list.append(".")
                
                #print("ascii_char:", ascii_char)
                #print("DD output_list:", output_list)
            #去掉回车换行
            elif (True == output_set_dict["delete_line_breaks"]) and (input_str[j] in ["\r", "\n"]) :
                j += 1
            #去掉空格
            elif (True == delimiter_set_dict["delete_space"]) and (input_str[j] in [" "]) :
                j += 1
            #去掉逗号,
            elif (True == delimiter_set_dict["delete_comma"]) and (input_str[j] in [","]) :
                j += 1
            #去掉句号.
            elif (True == delimiter_set_dict["delete_period"]) and (input_str[j] in ["."]) :
                j += 1
            #无法识别的就原样赋值
            else:
                output_list.append(input_str[j])
                j += 1

        #print("5.finish output_list:", output_list)


        #ascii-->16
        # 测试数据
        # "a"
        # "ab"
        # "abc"
        # "abc123"
        # "a b c"
        # "a,b,c"
        # "a.b.c"
        # "abc\r\ndef"
    def ascii_to_hex_fun(self, input_str, output_list, output_set_dict, delimiter_set_dict) :
        if 1 > len(input_str) :
            QMessageBox.critical(self, "警告", "输入为空,请输入有效数据", QMessageBox.Cancel, QMessageBox.Cancel)
            print("input_str is null")
            return
        #for test
        #input_str_len = len(input_str)

        for tmp_char in input_str :     #遍历每个字符
            if tmp_char in ASCII_OR_NUM_CHAR_SET :  #仅仅识别这个字符集里面的字符,若要处理可以在这个字符集里面新加
                if True == output_set_dict["display_prefix"] :  #显示前缀
                    hex_str = str(hex(ord(tmp_char)))   #ord返回的是字符对应的十进制, hex是返回16进制数形式是0x带前缀
                    output_list.append(hex_str)
                elif True == output_set_dict["not_display_prefix"] : #不显示前缀
                    hex_str = str(hex(ord(tmp_char)))
                    no_profix_hex = hex_str[2] + hex_str[3]
                    output_list.append(no_profix_hex)
                else :  #默认是显示前缀的
                    hex_str = str(hex(ord(tmp_char)))
                    output_list.append(hex_str)
                    #for test
                    #print("tmp_char:", tmp_char)
                    #print("ord(tmp_char):", ord(tmp_char))
                    #print("hex(ord(tmp_char)):", hex(ord(tmp_char)))
                    #print("str(hex(ord(tmp_char))):", str(hex(ord(tmp_char))))
                
                #有空格
                if True == delimiter_set_dict["add_space"] : 
                    output_list.append(" ")
                #有逗号
                if True == delimiter_set_dict["add_comma"] : 
                    output_list.append(",")
                #有句号
                if True == delimiter_set_dict["add_period"] : 
                    output_list.append(".")
            #去掉回车换行
            elif (True == output_set_dict["delete_line_breaks"]) and (tmp_char in ["\r", "\n"]) :
                continue
            #去掉空格
            elif (True == delimiter_set_dict["delete_space"]) and (tmp_char in [" "]) :
                continue
            #去掉逗号,
            elif (True == delimiter_set_dict["delete_comma"]) and (tmp_char in [","]) :
                continue
            #去掉句号.
            elif (True == delimiter_set_dict["delete_period"]) and (tmp_char in ["."]) :
                continue
            else:
                output_list.append(tmp_char)#无法识别的就原样赋值

        #only format
    def only_format_fun(self, input_str, output_list, output_set_dict, delimiter_set_dict) :
        if 1 > len(input_str) :
            QMessageBox.critical(self, "警告", "输入为空,请输入有效数据", QMessageBox.Cancel, QMessageBox.Cancel)
            print("input_str is null")
            return

        input_len = len(input_str)
        j = 0
        while  j < input_len : #遍历每个字符
            #删除回车换行
            if (True == output_set_dict["delete_line_breaks"]) and (input_str[j] in ["\r", "\n"]) :
                j += 1
                continue
            #去掉前缀
            elif (True == output_set_dict["not_display_prefix"]) and (input_str[j] in ["0"] and input_str[j+1] in ["x", "X"]) :
                j += 2
                continue
            #去掉空格
            elif (True == delimiter_set_dict["delete_space"]) and (input_str[j] in [" "]) :
                j += 1
                continue
            #去掉逗号,
            elif (True == delimiter_set_dict["delete_comma"]) and (input_str[j] in [","]) :
                j += 1
                continue
            #去掉句号.
            elif (True == delimiter_set_dict["delete_period"]) and (input_str[j] in ["."]) :
                j += 1
                continue
            else:
                #增加显示前缀
                if (True == output_set_dict["display_prefix"]) and (input_str[j] in HEX_CHAR_SET and input_str[j+1] in HEX_CHAR_SET) :
                    output_list.append("0x" + input_str[j] + input_str[j+1])
                    j += 2
                #无法识别的就原样赋值
                else :
                    output_list.append(input_str[j])
                    j += 1
                
                #增加空格
                if True == delimiter_set_dict["add_space"] : 
                    output_list.append(" ")
                #增加逗号
                if True == delimiter_set_dict["add_comma"] : 
                    output_list.append(",")
                #增加句号
                if True == delimiter_set_dict["add_period"] : 
                    output_list.append(".")

        #show asc table
    def show_ascii_table_fun(self, input_str, output_list):
        filenames = "ascii_table.txt"
        
        file_obj = open(filenames, "r", encoding="utf-8")
        output_list.append(file_obj.read()) 
        file_obj.close()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    main_p = Main_process()
    main_p.show()
    sys.exit(app.exec_())

3. ascii_table.txt文件

十进制	十六进制	MCS字符或缩写	DEC多国字符名
===========   ASCII控制字符   ===========
0	0	NUL	空字符
1	1	SOH	标题起始 (Ctrl/A)
2	2	STX	文本起始 (Ctrl/B)
3	3	ETX	文本结束 (Ctrl/C)
4	4	EOT	传输结束 (Ctrl/D)
5	5	ENQ	询问 (Ctrl/E)
6	6	ACK	认可 (Ctrl/F)
7	7	BEL	铃 (Ctrl/G)
8	8	BS	退格 (Ctrl/H)
9	9	HT	水平制表栏 (Ctrl/I)
10	0A	LF	换行 (Ctrl/J)
11	0B	VT	垂直制表栏 (Ctrl/K)
12	0C	FF	换页 (Ctrl/L)
13	0D	CR	回车 (Ctrl/M)
14	0E	SO	移出 (Ctrl/N)
15	0F	SI	移入 (Ctrl/O)
16	10	DLE	数据链接丢失 (Ctrl/P)
17	11	DC1	设备控制 1 (Ctrl/Q)
18	12	DC2	设备控制 2 (Ctrl/R)
19	13	DC3	设备控制 3 (Ctrl/S)
20	14	DC4	设备控制 4 (Ctrl/T)
21	15	NAK	否定接受 (Ctrl/U)
22	16	SYN	同步闲置符 (Ctrl/V)
23	17	ETB	传输块结束 (Ctrl/W)
24	18	CAN	取消 (Ctrl/X)
25	19	EM	媒体结束 (Ctrl/Y)
26	1A	SUB	替换 (Ctrl/Z)
27	1B	ESC	换码符
28	1C	FS	文件分隔符
29	1D	GS	组分隔符
30	1E	RS	记录分隔符
31	1F	US	单位分隔符

===========   ASCII特殊和数字字符   ===========
32	20	SP	空格
33	21	!	感叹号
34	22	"	引号 (双引号)
35	23	#	数字符号
36	24	$	美元符
37	25	%	百分号
38	26	&	和号
39	27	'	省略号 (单引号)
40	28	(	左圆括号
41	29	)	右圆括号
42	2A	*	星号
43	2B	+	加号
44	2C	,	逗号
45	2D	--	连字号或减号
46	2E	.	句点或小数点
47	2F	/	斜杠
48	30	049	31	1	1
50	32	2	2
51	33	3	3
52	34	4	4
53	35	5	5
54	36	6	6
55	37	7	7
56	38	8	8
57	39	9	9
58	3A	:	冒号
59	3B	;	分号
60	3C	<	小于
61	3D	=	等于
62	3E	>	大于
63	3F	?	问号

===========   ASCII字母字符   ===========
64	40	@	商业 at 符号
65	41	A	大写字母 A
66	42	B	大写字母 B
67	43	C	大写字母 C
68	44	D	大写字母 D
69	45	E	大写字母 E
70	46	F	大写字母 F
71	47	G	大写字母 G
72	48	H	大写字母 H
73	49	I	大写字母 I
74	4A	J	大写字母 J
75	4B	K	大写字母 K
76	4C	L	大写字母 L
77	4D	M	大写字母 M
78	4E	N	大写字母 N
79	4F	O	大写字母 O
80	50	P	大写字母 P
81	51	Q	大写字母 Q
82	52	R	大写字母 R
83	53	S	大写字母 S
84	54	T	大写字母 T
85	55	U	大写字母 U
86	56	V	大写字母 V
87	57	W	大写字母 W
88	58	X	大写字母 X
89	59	Y	大写字母 Y
90	5A	Z	大写字母 Z
91	5B	[	左中括号
92	5C	\	反斜杠
93	5D	]	右中括号
94	5E	^	音调符号
95	5F	_	下划线
96	60	`	重音符
97	61	a	小写字母 a
98	62	b	小写字母 b
99	63	c	小写字母 c
100	64	d	小写字母 d
101	65	e	小写字母 e
102	66	f	小写字母 f
103	67	g	小写字母 g
104	68	h	小写字母 h
105	69	i	小写字母 i
106	6A	j	小写字母 j
107	6B	k	小写字母 k
108	6C	l	小写字母 l
109	6D	m	小写字母 m
110	6E	n	小写字母 n
111	6F	o	小写字母 o
112	70	p	小写字母 p
113	71	q	小写字母 q
114	72	r	小写字母 r
115	73	s	小写字母 s
116	74	t	小写字母 t
117	75	u	小写字母 u
118	76	v	小写字母 v
119	77	w	小写字母 w
120	78	x	小写字母 x
121	79	y	小写字母 y
122	7A	z	小写字母 z
123	7B	{	左大括号
124	7C	|	垂直线
125	7D	}	右大括号 (ALTMODE)
126	7E	~	代字号 (ALTMODE)
127	7F	DEL	擦掉 (DELETE)

===========   控制字符   ===========
128	80		[保留]
129	81		[保留]
130	82		[保留]
131	83		[保留]
132	84	IND	索引
133	85	NEL	下一行
134	86	SSA	被选区域起始
135	87	ESA	被选区域结束
136	88	HTS	水平制表符集
137	89	HTJ	对齐的水平制表符集
138	8A	VTS	垂直制表符集
139	8B	PLD	部分行向下
140	8C	PLU	部分行向上
141	8D	RI	反向索引
142	8E	SS2	单移 2
143	8F	SS3	单移 3
144	90	DCS	设备控制字符串
145	91	PU1	专用 1
146	92	PU2	专用 2
147	93	STS	设置传输状态
148	94	CCH	取消字符
149	95	MW	消息等待
150	96	SPA	保护区起始
151	97	EPA	保护区结束
152	98		[保留]
153	99		[保留]
154	9A		[保留]
155	9B	CSI	控制序列引导符
156	9C	ST	字符串终止符
157	9D	OSC	操作系统命令
158	9E	PM	秘密消息
159	9F	APC	应用程序
===========   其他字符   ===========
160	A0		[保留] 2
161	A1	¡	反向感叹号
162	A2	¢	分币符
163	A3	£	英磅符
164	A4		[保留] 2
165	A5	¥	人民币符
166	A6		[保留] 2
167	A7	§	章节符
168	A8	¤	通用货币符号 2
169	A9	©	版权符号
170	AA	ª	阴性顺序指示符
171	AB	«	左角引号
172	AC		[保留] 2
173	AD		[保留] 2
174	AE		[保留] 2
175	AF		[保留] 2
176	B0	°	温度符
177	B1	±	加/减号
178	B2	²	上标 2
179	B3	³	上标 3
180	B4		[保留] 2
181	B5	µ	微符
182	B6	¶	段落符,pilcrow
183	B7	·	中点
184	B8		[保留] 2
185	B9	¹	上标 1
186	BA	º	阳性顺序指示符
187	BB	»	右角引号
188	BC	¼	分数四分之一
189	BD	½	分数二分之一
190	BE		[保留] 2
191	BF	¿	反向问号
192	C0	À	带重音符的大写字母 A
193	C1	Á	带尖锐重音的大写字母 A
194	C2	Â	带音调符号的大写字母 A
195	C3	Ã	带代字号的大写字母 A
196	C4	Ä	带元音变音 (分音符号) 的大写字母 A
197	C5	Å	带铃声的大写字母 A
198	C6	Æ	大写字母 AE 双重元音
199	C7	Ç	带变音符号的大写字母 C
200	C8	È	带重音符的大写字母 E
201	C9	É	带尖锐重音的大写字母 E
202	CA	Ê	带音调符号的大写字母 E
203	CB	Ë	带元音变音 (分音符号) 的大写字母 E
204	CC	Ì	带重音符的大写字母 I
205	CD	Í	带尖锐重音的大写字母 I
206	CE	Î	带音调符号的大写字母 I
207	CF	Ï	带元音变音 (分音符号) 的大写字母 I
208	D0		[保留] 2
209	D1	Ñ	带代字号的大写字母 N
210	D2	Ò	带重音符的大写字母 O
211	D3	Ó	带尖锐重音的大写字母 O
212	D4	Ô	带音调符号的大写字母 O
213	D5	Õ	带代字号的大写字母 O
214	D6	Ö	带元音变音 (分音符号) 的大写字母 O
215	D7	OE	大写字母 OE 连字 2
216	D8	Ø	带斜杠的大写字母 O
217	D9	Ù	带重音符的大写字母 U
218	DA	Ú	带尖锐重音的大写字母 U
219	DB	Û	带音调符号的大写字母 U
220	DC	Ü	带元音变音 (分音符号) 的大写字母 U
221	DD	Y	带元音变音 (分音符号) 的大写字母 Y
222	DE		[保留] 2
223	DF	ß	德语高调小写字母 s
224	E0	à	带重音符的小写字母 a
225	E1	á	带尖锐重音的小写字母 a
226	E2	â	带音调符号的小写字母 a
227	E3	ã	带代字号的小写字母 a
228	E4	ä	带元音变音 (分音符号) 的小写字母 a
229	E5	å	带铃声的小写字母 a
230	E6	æ	小写字母 ae 双重元音
231	E7	ç	带变音符号的小写字母 c
232	E8	è	带重音符的小写字母 e
233	E9	é	带尖锐重音的小写字母 e
234	EA	ê	带音调符号的小写字母 e
235	EB	ë	带元音变音 (分音符号) 的小写字母 e
236	EC	ì	带重音符的小写字母 i
237	ED	í	带尖锐重音的小写字母 i
238	EE	î	带音调符号的小写字母 i
239	EF	ï	带元音变音 (分音符号) 的小写字母 i
240	F0		[保留] 2
241	F1	ñ	带代字号的小写字母 n
242	F2	ò	带重音符的小写字母 o
243	F3	ó	带尖锐重音的小写字母 o
244	F4	ô	带音调符号的小写字母 o
245	F5	õ	带代字号的小写字母 o
246	F6	ö	带元音变音 (分音符号) 的小写字母 o
247	F7	oe	小写字母 oe 连字 2
248	F8	ø	带斜杠的小写字母 o
249	F9	ù	带重音符的小写字母 u
250	FA	ú	带尖锐重音的小写字母 u
251	FB	û	带音调符号的小写字母 u
252	FC	ü	带元音变音 (分音符号) 的小写字母 u
253	FD	ÿ	带元音变音 (分音符号) 的小写字母 y 2
254	FE		[保留] 2
255	FF		[保留] 2

4. 文件结构

在这里插入图片描述

5. 自测

请添加图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值