-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyteInterpreter.py
More file actions
28 lines (23 loc) · 873 Bytes
/
Copy pathbyteInterpreter.py
File metadata and controls
28 lines (23 loc) · 873 Bytes
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
import binascii
#EXTERNAL : Boiler plate code
def textToBits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))
def textToByteTable(text, split=32):
bits = textToBits(text)
table = []
for i in range(len(bits)//split):
table.append(bits[i*split:split*(i+1)])
return table
def textFromBits(bits, encoding='utf-8', errors='surrogatepass'):
string = ""
for i in range(len(bits)//8):
try:
string = string + getChar(bits[i*8:(i+1)*8], encoding, errors)
except Exception as e:
print(e)
string = string + "-"
return string
def getChar(byte, encoding, errors):
n = int(byte,2)
return n.to_bytes((n.bit_length()+7) // 8, 'big').decode(encoding, errors) or '\0'