-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathASCII_conv_hex.py
More file actions
71 lines (57 loc) · 1.66 KB
/
ASCII_conv_hex.py
File metadata and controls
71 lines (57 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Converter:
@staticmethod
# 比如这里实参只能是不包含前导符0x的十六进制数(0-F)的字符串,如'6805000B0212'
# 可以转换成以这些十六进制数为ASCII码值所对应的字符串返回
def hex_to_ascii(h):
"""
转换成ASCII码值对应的字符串
这次使用我是将字符串'6805000B0212'转换,其中的68转换成h
因为68对应的十进制为104
104对应的ascii字符为h
:return str类型: h (,
"""
list_s = []
# i每次增长2,len为字符串长度
for i in range(0, len(h), 2):
# upper()是转换为大写
# append()是在方法用于在列表(List)末尾添加新的对象
list_s.append(chr(int(h[i:i+2].upper(), 16)))
return ''.join(list_s)
@staticmethod
def str_to_hexstr(s):
list_h = []
for c in s:
# ord()返回单个字符的ASCII码值,如字符a返回的是97
list_h.append(str(hex(ord(c)))[-2:]) # 取hex转换16进制的后两位
return ''.join(list_h)
"""
a1 = Converter.hex_to_ascii('6805000B0212')
b1 = a1.encode()
decode_b1 = b1.decode()
# 跟上面一样的效果
a2 = '\x68\x05\x00\x0B\x02\x12'
b2 = a2.encode()
d1 = Converter.str_to_hex(a1)
c2 = bytes.fromhex('6805000B0212')
d2 = bytes.hex(c2)
e = a1
print(a1)
"""
a = b'h'
b = b'h\x69'
c = b[1:] == b'i'
a = chr(68)
a = Converter.str_to_hexstr('hi')
a = '680400282C'
b = a.encode()
clear_Sensor_angle = bytes.hex(b'h')
b = clear_Sensor_angle.encode()
a = 'h' + 'ello'
b = a.encode()
c = 'hello'
d = c.encode()
e = b'h\x02\x1f'
i = 2
f = b'\x02\x1f'
g = b'h'
h = g + f