-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharmap.py
27 lines (20 loc) · 836 Bytes
/
Charmap.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
class Charmap:
def __init__(self, charmap):
self.ByteToChar = {}
self.CharToByte = {}
for char, byte in charmap.items():
if char not in self.CharToByte:
self.CharToByte[char] = []
if byte not in self.ByteToChar:
self.ByteToChar[byte] = []
self.ByteToChar[byte].append(char)
self.CharToByte[char].append(byte)
def toChar(self, bytes):
if type(bytes) != list:
bytes = [bytes]
return "".join([self.ByteToChar[byte][0] for byte in bytes])
# TODO - need to handle multi-character 'chars'
def toBytes(self, chars):
if type(chars) != list:
chars = [chars]
return [self.CharToByte[char][0] for char in chars]