generated from mwc/lab_compression
Revised the ascii5.py and tested it on python.
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
from custom_codecs.register import register_codec
|
from text_codecs.register import register_codec
|
||||||
from easybits import Bits
|
from easybits import Bits
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
|
||||||
chars = string.ascii_uppercase + " "
|
chars = string.ascii_uppercase + " "
|
||||||
char_to_num = {c: i for i, c in enumerate(chars)}
|
char_to_num = {c: i for i, c in enumerate(chars)}
|
||||||
num_to_char = {i: c for i, c in enumerate(chars)}
|
num_to_char = {i: c for i, c in enumerate(chars)}
|
||||||
@@ -14,7 +13,7 @@ def encode(text):
|
|||||||
for ch in text:
|
for ch in text:
|
||||||
if ch in char_to_num:
|
if ch in char_to_num:
|
||||||
num = char_to_num[ch]
|
num = char_to_num[ch]
|
||||||
b = Bits(uint=num, length=5)
|
b = Bits(format(num, "05b"))
|
||||||
result = result.concat(b)
|
result = result.concat(b)
|
||||||
|
|
||||||
|
|
||||||
@@ -31,11 +30,10 @@ def decode(data):
|
|||||||
chunk = bits[i:i+5]
|
chunk = bits[i:i+5]
|
||||||
if len(chunk) < 5:
|
if len(chunk) < 5:
|
||||||
break
|
break
|
||||||
num = chunk.uint
|
num = int(str(chunk), 2)
|
||||||
if num in num_to_char:
|
if num in num_to_char:
|
||||||
text += num_to_char[num]
|
text += num_to_char[num]
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
register_codec(encode, decode, "ascii5")
|
register_codec(encode, decode, "ascii5")
|
||||||
|
|||||||
Reference in New Issue
Block a user