Decoding Google’s First Tweet in Python

2009 February 28
tags:
by Arun Bhai

Most of you must have read the news that Google finally jumped into the Twitter Bandwagon. In their trademark style, they have chosen to announce this in a cryptic way. Their first tweet was essentially this:

I’m 01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010

I will explain in this post how to crack this simple code with the help of some Python one-liners (Google’s favourite language). If you are a Google aspirant (who isn’t? ;) ), this might help you clear the interview. So pay attention.

To most people it is immediately obvious that it is a text encoded in binary. Since each binary word is 8 characters long, it is most probably written in the extended 8-bit ASCII code. In fact, it is and you can read this with a simple ASCII chart.

But they have made it slightly difficult for you by writing in binary. Since most charts would provide you a lookup from decimal or hexadecimal numbers to ASCII representations only. So how do you convert from binary to decimal? It’s quite simple:

decimal = lambda s: sum(int(j) * pow(2,i) for i,j in enumerate(reversed(s)))

This line defines a function decimal which works in a manner similar to how we would manually convert binary numbers into decimal. Each position is multiplied by increasing powers of two from the right. Then, these numbers are added together. for e.g. ‘1010′ will be 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 10.

Next, we split the binary part of the tweet string and apply the decimal function on each part

tweet = "01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010"
print ''.join(chr(decimal(s)) for s in tweet.split())

The result is something that you might have already guessed seeing the first 2 words:

“I’m feeling lucky\n”

Hope you learnt some interesting python constructs. If there are other ways of decoding this in Python, please comment below.

Distantly Related posts:

  1. Is Python Intellisense Possible in Emacs?
  2. Making Python Scripts Show Windows-friendly Errors/Stacktrace
  3. Windows+Python Integration Like Unix shell
  4. Mars Rover in Python and Haskell
  5. Work Faster in Windows With Launchy and a few Python Scripts
13 Responses leave one →
  1. February 28, 2009

    Try:

    decimal(”01100110″) == int(”01100110″, 2)

  2. buge permalink
    February 28, 2009

    Not a one-liner but something that I hacked together the other day in python. I was messing around with a friend and we were sending each other both 7-bit ascii characters separated by spaces as well as 8-bit ascii characters all joined. So the script can handle both:

    from sys import stdin, stdout

    byte = '' string = '' while True: buffer = stdin.read(1) if not buffer: break show = False if buffer not in '01': show = True else: byte += buffer if (show and byte) or len(byte) == 8: stdout.write(chr(int(byte, 2))) byte = ''

  3. ThomasWaldmann permalink
    February 28, 2009

    try: int(”101″, 2)

  4. Georg Brandl permalink
    February 28, 2009

    Actually, decimal(s) is builtin: int(s, 2).

  5. Andrew Dalke permalink
    February 28, 2009

    Another construct – ‘int’ takes an optional base. In this case, base 2:

    That is, your ‘decimal(s)’ is the same as ‘int(s, 2)’

    “”.join(chr(int(s, 2)) for s in tweet.split())

  6. February 28, 2009

    @Simon Wittber, @ThomasWaldmann, @Georg Brandl: Thanks a lot for the much simpler solution. I forgot about int’s capability to perform conversion to any base. In fact, I was looking for a standard function to do this, but in all the wrong places like binascii.

    @buge: Thanks a lot for posting the code. But I am not sure how it works. Will check it out with some test files.

  7. March 1, 2009

    txt = “”"01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010″”" ”.join(chr(int(x, 2)) for x in txt.split()) ‘feeling luckyn’

  8. March 1, 2009

    Python 2.6 and 3.0 have the new binary literals:

    0b01100110 198

  9. Varun Thacker permalink
    March 1, 2009

    nice post.Bookmarked!

    My blog>

  10. March 4, 2009

    :roll:

  11. March 8, 2009

    @Heikki Toivonen:

    Brilliant! This was exactly what I was looking for :)

Trackbacks and Pingbacks

  1. Binari [001]
  2. Google’s cryptic tweet : What does it really means? | Nikhil Vishnu

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Additional comments powered by BackType