How to translate the binary email from Swarthmore

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

About 10 ** -n and 0.1 ** n

Normally, in mathematics, we know that 10 powered by -n equals to 0.1 powered by n.
However, in python, this equation can not stand well.

Recently, I’m working on a project, that needs the calculation of gravity between two planets. As it’s known to all, the gravitational constant G = 6.67 * 10 ^ -11. According to the formula, I write a line of code which is grav_cons = 6.67 * 0.1 ** 11. However, when I executed my code, the answer went wrong. The value of gravity in several digits after the decimal point weren’t correct.
After that, I spent serveral hours checking my code, and finally found the mistake.
In python, the answer of a calculation that contains float value will always be a float value. However, in the piece of code, the computer just automatically convert the decimal values into binaries and do the calculations. After that, the computer convert the answer in binary into the answer in decimal system and return the value, which leads to a calculation errors and effect the answer. In the end, I change the code into 10**-11, and the program finally went correct.

^ in Python

I remembered when I started to learn Python, about 1 year ago, I often get confused with ^ and **.
With my poor computer basics, I often type ^ when I was trying to explain power, and I found out the output is quite strange. Later on, I just memorized ** and never asked about ^.
Until today, I finally solved the mystery.

The Outputs When I Use ^

^ represents XFOR in python, which add two integers in binary system with limited digits.

For example, when the computer do 4^1, it first converts the two numbers into binary numbers, which are 100 and 1. Secondly, it calculate 100 + 1 = 101, which is 5.
However in the third case, when the computer add 110 + 110, the result is 000. Thats because, when the programmer inputs 6^6, the computer automatically sets the binary digits of answer into the highest binary digits of input number. As the highest digits of the input number is 3, the value of the results is out of limits, which is called overflow. In the end, computer ouput the results of 000 in binary, which is 0