2014년 9월 5일 금요일

Quick Solution to a binary-to-ASCII puzzle in a ShortCutFoo Marketing Email

This morning I received the following email in my inbox:

This Week in Bits!

"All the news that's fit to 01110000 01110010 01101001 01101110 01110100"
01010111 01100101 01101100 01101100 00101100 00100000 01001001 00100000 01100100 01101111 01101110 00100111 01110100 00100000 01101011 01101110 01101111 01110111 00100000 01110111 01101000 01111001 00100000 01110111 01100101 00100000 01110100 01101000 01101111 01110101 01100111 01101000 01110100 00100000 01110100 01101000 01101001 01110011 00100000 01110111 01100001 01110011 00100000 01100001 00100000 01100111 01101111 01101111 01100100 00100000 01101001 01100100 01100101 01100001 00101110 00100000 00100000 01001111 01110010 00100000 01110111 01101000 01101111 00100000 01110111 01101111 01110101 01101100 01100100 00100000 01110010 01100101 01100001 01100100 00100000 01101001 01110100 00101110 00100000 00100000 01000010 01110101 01110100 00100000 01111001 01101111 01110101 00100000 01100100 01101001 01100100 00101110 00100000 00100000 01000010 01100101 01100011 01100001 01110101 01110011 01100101 00100000 01111001 01101111 01110101 00100111 01110010 01100101 00100000 01100001 01110111 01100101 01110011 01101111 01101101 01100101 00101110 00100000 00100000 01001001 00100111 01100100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01100010 01110101 01111001 00100000 01111001 01101111 01110101 00100000 01100001 00100000 01100010 01100101 01100101 01110010 00100000 01101001 01100110 00100000 01111001 01101111 01110101 00100000 01110111 01100101 01110010 01100101 00100000 01101000 01100101 01110010 01100101 00101110 00100000 01000001 01110011 01110011 01110101 01101101 01101001 01101110 01100111 00100000 01110100 01101000 01100001 01110100 00100000 01100010 01100101 01100101 01110010 00100000 01110111 01100001 01110011 00100000 01100001 01110100 00100000 01100001 00100000 01110010 01100101 01100001 01110011 01101111 01101110 01100001 01100010 01101100 01100101 00100000 01110000 01110010 01101001 01100011 01100101 00101110 00100000 01010100 01101000 01100101 01110010 01100101 00100111 01110011 00100000 01100001 00100000 01110000 01101100 01100001 01100011 01100101 00100000 01101001 01101110 00100000 01101101 01111001 00100000 01101110 01100101 01101001 01100111 01101000 01100010 01101111 01110010 01101000 01101111 01101111 01100100 00100000 01110100 01101000 01100001 01110100 00100000 01110011 01100101 01101100 01101100 01110011 00100000 01101110 01101111 01110010 01101101 01100001 01101100 00100000 01110011 01101001 01111010 01100101 01100100 00100000 01100010 01100101 01100101 01110010 01110011 00100000 01100110 01101111 01110010 00100000 00110001 00110010 00100000 01100100 01101111 01101100 01101100 01100001 01110010 00101110 00100000 00100000 00110001 00110010 00100000 01100100 01101111 01101100 01101100 01100001 01110010 01110011 00100001 00100000 00100000 01001001 01110100 00100111 01110011 00100000 01101110 01110101 01110100 01110011 00100001 00100000 00100000 01000001 01101110 01111001 01110111 01100001 01111001 00101110 00100000 00101110 00100000 00101110

Python has nice built-in functions for dealing with numbers in various bases. For example, bin() will convert a decimal number into a binary representation prefixed with 0b. Although ASCII was originally a 7-bit format, nowadays the leftmost or 8th bit (in the \(2^8\) position) is customarily set to 0, so all our strings of binary numbers will have a total of eight characters - the first character from the left is 0, while the remaining 7 characters represent 7-bit binary.

I first wrote a quick-and-dirty version to see what the message above was saying. Then I sat down and wrote a more formal version encapsulating code into the functions cvBinToASCII() and cvASCIItoBin() complete with doctests.

Sending a marketing email in binary is a great idea on ShortCutFoo's part for selling services to coders and those interested in programming!