So firstly I have two lists :
l1 = [chr(x) for x in range(32,160)]
l2 = [bin(x)[2:].rjust(7,'0') for x in range(128)]
The first one list 128 characters, and the second 128 binary values (7 bits so from 0000000 to 1111111).
The idea is to convert any string to its binary value. So my original code is :
def encode(message):
for a, b in zip(l1,l2):
message = message.replace(a, b)
return message
Sadly I'm getting errors on characters previously the '0' char. Example :
Input : ~
Output : 1011110
Input : ♥
Output : 0011111
Input : a
Output : 1000001
Input : 0
Output : 0000100010000
Input :
Output : 00001000100000000100010000000010001000000001000100000000100010000000010
00100000000100010000
Input : 4
Output : 00001000100000000100010000000010001000000001000100000000100010000000010
001000000001000100000010100
Input : 1
Output : 00001000100000000100010000000010001000000001000100000000100010000000010
001000000001000100000010001
Input : 0
Output : 00001000100000000100010000000010001000000001000100000000100010000000010
001000000001000100000000100010000
I'm sure that this is related to the 0 conversion because when doing this following code it works.
def encode(message):
for a, b in zip(l1,l2):
print message.replace(a, b)
return message
How can I fix it? Additionally as we can see the ♥ has been converted, but it should have not. What is going on? What I need is 128 different bytes of 7 bits, printable and all (for cryptography).