From f93ad4e178f5a0df2b19394fd288a2ffe3101c5e Mon Sep 17 00:00:00 2001 From: tang-dawei Date: Fri, 9 Apr 2021 15:26:59 +0800 Subject: [PATCH] =?UTF-8?q?format=5Fstring=E5=88=9D=E7=A8=BF=EF=BC=9A?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=8EHEX?= =?UTF-8?q?=E3=80=81=E4=BA=8C=E8=BF=9B=E5=88=B6=E4=B8=8EASCII=E3=80=81stru?= =?UTF-8?q?ct=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/FAQ/zh/Format_String/Format_String.md | 66 +++++++++++++++++++--- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/docs/FAQ/zh/Format_String/Format_String.md b/docs/FAQ/zh/Format_String/Format_String.md index f845f33..656aa4e 100644 --- a/docs/FAQ/zh/Format_String/Format_String.md +++ b/docs/FAQ/zh/Format_String/Format_String.md @@ -4,11 +4,11 @@ | ---- | --------- | ----- | ------------------------------------------------------------ | | 1.0 | 2021-4-07 | David | 首次编写,包括位运算、字符串与十六进制转换、以及Struct的说明 | -对于字符串与十六进制之间的转换需求,特编写这篇文档,详细介绍相关知识 +对于字符串与十六进制之间的转换需求,特编写这篇文档,详细介绍相关知识。 ## 位运算 -位运算,计算机内所有的数都以二进制存储,位运算就是对二进制位的操作。在python中主要包括:按位与运算符、按位或运算符、 按位异或运算符、按位取反运算符、左移动运算符、右移动运算符 +首先介绍位运算,计算机内所有的数都以二进制存储,位运算就是对二进制位的操作。在python中主要包括:按位与运算符、按位或运算符、 按位异或运算符、按位取反运算符、左移动运算符、右移动运算符。具体的介绍可参考:[位运算](https://blog.csdn.net/csdn_edition/article/details/109402978) ### 按位与运算符 @@ -73,11 +73,38 @@ 15 ``` -还有位运算,struct的用法,所以写个demo还是有必要的哦。 +还有位运算,struct的用法,所以写个demo还是有必要的哦。这是一项通过将设备置于从高温到低温的快速改变环境中或反之变换来测试机器和焊接部分的可靠性 ## 字符串与十六进制 -python客户端与服务端(c程序)进行通讯,需接收服务端发来的16进制码流,并对16进制数据进行解码,得到相应字段的数据,并可以将数据打包成对应格式的码流发送给服务端,多字节整数传输采用网络字节序。此处介绍一下字符串转换成十六进制的方法: +### 字符串与十六进制转换的封装 + +对于字符串与十六进制之间的相互转换,由于python没有对应的封装,这里封装一个类供大家使用: + +```python +str_test = 'Quectel build a smarter world' +class String: + def to_hex(a, b=""): # 可实现的功能:转成HEX格式,可选择加上需要的分隔符 + Hex = ''.join([hex(ord(c)).replace('0x', b) for c in a]) + return Hex,len(a) + + def from_hex(a,b=''): # 可实现的功能:转成STR,如果有分隔符,可以添加分割符参数转换 + Str = ''.join([chr(int(c.replace(b, ''), 16)) for c in [a[i:i+2+len(b)] for i in range(0, len(a), 2+len(b))]]) + return Str + +hex_test = String.to_hex(str_test) +print(type(hex_test[0])) +print(hex_test[1]) +print(hex_test) + +hex_test = str(hex_test[0]) +str_test = String.from_hex(hex_test) +print(str_test) +``` + +### 字符串与十六进制转换的应用(待完善) + +当然转换后的HEX无法串口直接接收解析,仍需要一定的转换,下面基于EC600SCNLB模组做个简单的实验 ```python str_test = 'Quectel build a smarter world' @@ -89,8 +116,6 @@ def str_to_hex(s): hex_test = str_to_hex(str_test) ``` -下面使用EC600SCNLB模组做个简单的实验 - ​ 1.编写如下代码并命名为str_hex_test.py。 ```python @@ -122,4 +147,31 @@ uart0.write(hex_test) ​ 4.对于模组的串口接收:无论串口工具发送ASCII或者HEX数据,串口读取都是bytes型数据 -![image-20210407151559820](media/uart_read.png) \ No newline at end of file +![image-20210407151559820](media/uart_read.png) + +## 编解码 + +对于编解码,常见的有ASCII、UTF-8、GBK等等,python已经有了对应的封装,这里简单的介绍一下,也可以参考 [encode and decode](https://blog.csdn.net/qq_26442553/article/details/94440502): + +```python +import ubinascii as binascii +import ustruct as struct + +def example(express, result=None): + if result == None: + result = eval(express) + print(express, ' ==> ', result) +if __name__ == '__main__': + print('编解码:') + print("str to utf-8", end=': '); + example("u'小明'.encode('utf-8')") + print("utf-8 to str", end=': '); + s_utf = b'\xe5\xb0\x8f\xe6\x98\x8e' + example("s_utf.decode('utf-8')") + print("类似的还有encode('gbk'),decode('gbk'),encode('gb2312'),decode('gb2312')") + +``` + +## 二进制与ASCII转换、打包与解压 + +对于[二进制与ASCII转换](https://python.quectel.com/wiki/#/zh-cn/api/pythonStdlib?id=ubinascii-二进制与ascii转换)和[ustruct - 打包和解压原始数据类型](https://python.quectel.com/wiki/#/zh-cn/api/pythonStdlib?id=ustruct-打包和解压原始数据类型),在模组的固件中已经有了封装。 \ No newline at end of file -- Gitee