Simple binary encoding in Python

Rationale

  • Use 64 characters (the 26 letters of the Latin alphabet in both lower and uppercase, the 10 digits, period and space)
  • Turn that character to a number
  • Represent that number in binary, padded to 8 bits for future use

Further work

  • A binary-to-string encoder
  • One byte allows for 256 different values, so this naive implementation only uses 1/4 of the potential space
  • Encoding to binary is easy because that’s how numbers are actually processed by the computer, but there are potentially infinite encodings (encoding to emoji, base65536, you name it.

The actual code

Somewhat readable, I hope

def char2bin(letter):
    '''Returns a naive, non-standard binary encoding of a single character. '''
    aleph = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. '
    if letter in aleph:
        result = format(aleph.index(letter), '08b') + ' '
    else:
        result = format(64, '08b') + ' '  # return 01000000 if character is not in the alphabet
    return result
#%
def str2bin(string, row=5):
    '''Returns a naive binary encoding of string.'''
    binaried = ''
    for letter in string:
        binaried += char2bin(letter)
    formatted = ''
    for idx, bit in enumerate(binaried):
        if idx % ((9 * row)) == 0 and idx > 0:
            formatted += '\n'
        formatted += bit
    return formatted
lord = '''
Our Father, which art in heaven,
Hallowed be thy name.
Thy kingdom come, thy will be done in earth as it is in heaven.
Give us this day our daily bread.
And forgive us our debts, as we forgive our debtors.
And lead us not into temptation, but deliver us from evil.
For thine is the kingdom, and the power, and the glory for ever.
Amen. 
'''
print(str2bin(lord))
01000000 00101000 00010100 00010001 00111111 
00011111 00000000 00010011 00000111 00000100 
00010001 01000000 00111111 00010110 00000111 
00001000 00000010 00000111 00111111 00000000 
00010001 00010011 00111111 00001000 00001101 
00111111 00000111 00000100 00000000 00010101 
00000100 00001101 01000000 01000000 00100001 
00000000 00001011 00001011 00001110 00010110 
00000100 00000011 00111111 00000001 00000100 
00111111 00010011 00000111 00011000 00111111 
00001101 00000000 00001100 00000100 00111110 
01000000 00101101 00000111 00011000 00111111 
00001010 00001000 00001101 00000110 00000011 
00001110 00001100 00111111 00000010 00001110 
00001100 00000100 01000000 00111111 00010011 
00000111 00011000 00111111 00010110 00001000 
00001011 00001011 00111111 00000001 00000100 
00111111 00000011 00001110 00001101 00000100 
00111111 00001000 00001101 00111111 00000100 
00000000 00010001 00010011 00000111 00111111 
00000000 00010010 00111111 00001000 00010011 
00111111 00001000 00010010 00111111 00001000 
00001101 00111111 00000111 00000100 00000000 
00010101 00000100 00001101 00111110 01000000 
00100000 00001000 00010101 00000100 00111111 
00010100 00010010 00111111 00010011 00000111 
00001000 00010010 00111111 00000011 00000000 
00011000 00111111 00001110 00010100 00010001 
00111111 00000011 00000000 00001000 00001011 
00011000 00111111 00000001 00010001 00000100 
00000000 00000011 00111110 01000000 00011010 
00001101 00000011 00111111 00000101 00001110 
00010001 00000110 00001000 00010101 00000100 
00111111 00010100 00010010 00111111 00001110 
00010100 00010001 00111111 00000011 00000100 
00000001 00010011 00010010 01000000 00111111 
00000000 00010010 00111111 00010110 00000100 
00111111 00000101 00001110 00010001 00000110 
00001000 00010101 00000100 00111111 00001110 
00010100 00010001 00111111 00000011 00000100 
00000001 00010011 00001110 00010001 00010010 
00111110 01000000 00011010 00001101 00000011 
00111111 00001011 00000100 00000000 00000011 
00111111 00010100 00010010 00111111 00001101 
00001110 00010011 00111111 00001000 00001101 
00010011 00001110 00111111 00010011 00000100 
00001100 00001111 00010011 00000000 00010011 
00001000 00001110 00001101 01000000 00111111 
00000001 00010100 00010011 00111111 00000011 
00000100 00001011 00001000 00010101 00000100 
00010001 00111111 00010100 00010010 00111111 
00000101 00010001 00001110 00001100 00111111 
00000100 00010101 00001000 00001011 00111110 
01000000 00011111 00001110 00010001 00111111 
00010011 00000111 00001000 00001101 00000100 
00111111 00001000 00010010 00111111 00010011 
00000111 00000100 00111111 00001010 00001000 
00001101 00000110 00000011 00001110 00001100 
01000000 00111111 00000000 00001101 00000011 
00111111 00010011 00000111 00000100 00111111 
00001111 00001110 00010110 00000100 00010001 
01000000 00111111 00000000 00001101 00000011 
00111111 00010011 00000111 00000100 00111111 
00000110 00001011 00001110 00010001 00011000 
00111111 00000101 00001110 00010001 00111111 
00000100 00010101 00000100 00010001 00111110 
01000000 00011010 00001100 00000100 00001101 
00111110 00111111 01000000