forked from xiongyihui/pqcom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpqcom_translator.py
51 lines (40 loc) · 1.34 KB
/
pqcom_translator.py
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
def from_hex_string(text):
return str(bytearray.fromhex(text.replace('\n', ' ')))
def from_extended_string(text):
return text.strip('\n').replace('\\n', '\n').replace('\\r', '\r')
def to_hex_prefix_string(text):
result = ''
hex_part = ''
str_part = ''
bytes_index = 0
while bytes_index < len(text):
ch = text[bytes_index]
if ch == '\n':
hex_part += '0A' + ' '
str_part += '.'
result += hex_part.ljust(52) + str_part + '\n'
hex_part = ''
str_part = ''
elif ch == '\r':
hex_part += '0D' + ' '
str_part += '.'
if ((bytes_index + 1) < len(text)) and (text[bytes_index + 1] == '\n'):
hex_part += '0A' + ' '
str_part += '.'
bytes_index += 1
result += hex_part.ljust(52) + str_part + '\n'
hex_part = ''
str_part = ''
else:
hex_part += '{:02X}'.format(ord(ch)) + ' '
if ch < '\x20' or ch > '\x7F':
ch = '.'
str_part += ch
if len(str_part) >= 16:
result += hex_part.ljust(52) + str_part + '\n'
hex_part = ''
str_part = ''
bytes_index += 1
if str_part:
result += hex_part.ljust(52) + str_part + '\n'
return result