-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii_helpers.py
70 lines (44 loc) · 1.36 KB
/
ascii_helpers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
BASE = 94
# pylint: disable=line-too-long
STRING_ASCII = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`|~ \n"
def ascii_to_index(msg: str) -> list[int]:
msg_bytes = msg.encode("ascii")
res: list[int] = []
for char in msg_bytes:
index = char - 33
res.append(index)
return res
def index_to_string(msg: list[int]) -> str:
res = ""
for elem in msg:
res += STRING_ASCII[elem]
return res
def string_to_index(msg: str) -> list[int]:
res: list[int] = []
for char in msg:
index = STRING_ASCII.index(char)
res.append(index)
return res
def index_to_ascii(msg: list[int]) -> str:
res = b""
for elem in msg:
res += bytes([elem + 33])
return res.decode("ascii")
def decode_integer(body: str) -> int:
indices = ascii_to_index(body)
result = 0
for i, elem in enumerate(reversed(indices)):
result += (BASE**i) * elem
return result
def decode_string(body: str) -> str:
indices = ascii_to_index(body)
return index_to_string(indices)
def encode_string(msg: str) -> str:
indices = string_to_index(msg)
return index_to_ascii(indices)
def encode_integer(num: int) -> str:
res: list[int] = []
while num > 0:
res.append(num % BASE)
num //= BASE
return index_to_ascii(res)