Hey guys, I just received a special email from Swarthmore College few minutes ago, and it looks like this:

In this post, I will simply demonstrate how to convert the binary values into English text through Python.
To begin with, at the beginning of the email, we can see there’s a space between two 8-bits binary numbers 01001000 and 01101001. 01001000 represent 72, which is the Ascii code of ’h’, and 01101001 represents 105, which is the Ascii code of ’i’. Thus, the rest of the email should be consisted of chars that are represented by 8-bits binary numbers as well.
So the first thing we need to do is write a Decoder that translates binary numbers to English:
def Decoder(binary_string):
ascii_code = int('0b'+ binary_string, 2) #Find the Ascii code
result = chr(ascii_code) #Find the char
return result
After finishing up this part, we need another function to split up the continues 8-bits binary numbers in the content and translate each of them individually:
def BinaryTranslator(binary_text):
total_number= len(binary_text)//8
text = ''
for count in range(total_number):
text += Decoder(binary_text[count*8:(count+1)*8])
return text
Caution: We use // instead of / in this situation. This is because / returns a float number, which could cause trouble in range function.
As we can see in this email, there are some letters that are represented through English letters. So I add a select structure here in order to let the program automatically returns the original text if it is already in English.
def BinaryTranslator(binary_text):
if binary_text[0] != '0' and binary_text[0] != '1':
return binary_text
binary_count = len(binary_text)//8
text = ''
for count in range(binary_count):
text += Decoder(binary_text[count*8:(count+1)*8])
return text
In the end, we put the modules together:
def Decoder(binary_string):
ascii_code = int('0b'+ binary_string, 2) #Find denary value
result = chr(ascii_code) #Find the char
return result
def BinaryTranslator(binary_text):
if binary_text[0] != '0' and binary_text[0] != '1':
return binary_text
binary_count = len(binary_text)//8
text = ''
for count in range(binary_count):
text += Decoder(binary_text[count*8:(count+1)*8])
return text
original_text = ['0100100001101001', 'Jiarui','01000010011010010110111001100001011100100111100100100000011000110110111101100100011001010010000001101001011100110010000001100001001000000110011001110101011011100010000001110111011000010111100100100000011101000110111100100000011000110110111101101101011011010111010101101110011010010110001101100001011101000110010100101110', '01000010011101010111010000100000011010010111010000100111011100110010000001110100011011110111010101100111011010000010000001110100011011110010000001110010011001010110000101100100001011000010000001110011011011110010000001110111011001010010011101101100011011000010000001101011011001010110010101110000001000000111010001101000011010010111001100100000011000100111001001101001011001010110011000101110', '010101110110100101101100011011000010000001111001011011110111010100100000011000010111000001110000011011000111100100100000011101000110111100100000010100110111011101100001011100100111010001101000011011010110111101110010011001010011111100001010', 'Sincerely,','010101000110100001100101001000000111011001100001011100100110100101101111011101010111001100100000011100000110010101101111011100000110110001100101001000000110111101100110001000000111010001101000011001010010000001010011011101110110000101110010011101000110100001101101011011110111001001100101001000000100000101100100011011010110100101110011011100110110100101101111011011100111001100100000010011110110011001100110011010010110001101100101']
for text in original_text:
print(BinaryTranslator(text))
If you run the program through Python IDE, you will get the message below:
Hi
Jiarui
Binary code is a fun way to communicate.
But it’s tough to read, so we’ll keep this brief.
Will you apply to Swarthmore?
Sincerely,
The various people of the Swarthmore Admissions Office




















