Exemplo n.º 1
0
Arquivo: gray.py Projeto: 47-/Cyprium
def do_decypher(text, codec=DEFAULT, length=8):
    """
    Function to convert Gray code into text.
    Note: expect "unspaced" binary text as input!
    """
    mapp = gray2bin_n(length)

    bytes = "".join(mapp[chunk] for chunk in utils.grouper2(text, length))
    # We want to get back to real bytes, hence stripping down dummy '0' we
    # added at encode time to get an integer number of length-words.
    new_len = len(bytes)
    new_len -= new_len % 8
    bytes = bytes[:new_len]
    # And now, convert those textual bytes back to real bytes!
    bytes = utils.int8_to_bytes(int(c, 2) for c in utils.grouper2(bytes, 8))
    return bytes.decode(codec)
Exemplo n.º 2
0
def do_decypher(text, codec=DEFAULT, length=8):
    """
    Function to convert Gray code into text.
    Note: expect "unspaced" binary text as input!
    """
    mapp = gray2bin_n(length)

    bytes = "".join(mapp[chunk] for chunk in utils.grouper2(text, length))
    # We want to get back to real bytes, hence stripping down dummy '0' we
    # added at encode time to get an integer number of length-words.
    new_len = len(bytes)
    new_len -= new_len % 8
    bytes = bytes[:new_len]
    # And now, convert those textual bytes back to real bytes!
    bytes = utils.int8_to_bytes(int(c, 2) for c in utils.grouper2(bytes, 8))
    return bytes.decode(codec)
Exemplo n.º 3
0
def do_decypher(text, codec=DEFAULT, base=2):
    """
    Function to convert binary/octal/decimal/hexadecimal text into text.
    Note: expect "unspaced" text as input!
    """
    n_digits = {k: v for k, v in N_DIGITS.items()}
    if codec == ASCII7:
        codec = ASCII
        n_digits[2] = 7

    if base != 16:
        ints = (int(''.join(p), base)
                for p in utils.grouper(text, n_digits[base], ''))
        byts = utils.int8_to_bytes(ints)
    else:
        byts = bytes.fromhex(text)
    return byts.decode(codec)
Exemplo n.º 4
0
def do_decypher(text, codec=DEFAULT, base=2):
    """
    Function to convert binary/octal/decimal/hexadecimal text into text.
    Note: expect "unspaced" text as input!
    """
    n_digits = {k: v for k, v in N_DIGITS.items()}
    if codec == ASCII7:
        codec = ASCII
        n_digits[2] = 7

    if base != 16:
        ints = (int(''.join(p), base)
                for p in utils.grouper(text, n_digits[base], ''))
        byts = utils.int8_to_bytes(ints)
    else:
        byts = bytes.fromhex(text)
    return byts.decode(codec)
Exemplo n.º 5
0
 def __bytes__(self):
     return utils.int8_to_bytes(self._list)