generated from mwc/lab_compression
30 lines
693 B
Python
30 lines
693 B
Python
from easybits import Bits
|
|
from register import register_codec
|
|
|
|
CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!"
|
|
|
|
def encode(text):
|
|
result = Bits()
|
|
text = text.upper()
|
|
for char in text:
|
|
if char in CHARACTERS:
|
|
index = CHARACTERS.index(char)
|
|
b = Bits(index, length=6)
|
|
result = result.concat(b)
|
|
return result.bytes
|
|
|
|
def decode(data):
|
|
bits = Bits(bytes(data))
|
|
text = ""
|
|
for i in range(0, len(bits), 6):
|
|
b = bits[i:i+6]
|
|
if len(b)<6:
|
|
break
|
|
|
|
index = b.int
|
|
if index < len(CHARACTERS):
|
|
text+= CHARACTERS[index]
|
|
return text
|
|
|
|
register_codec(encode, decode, "ascii6")
|