Created
June 7, 2017 06:00
-
-
Save samliu/4c63c1978e0a1cccd19b76ccd492382b to your computer and use it in GitHub Desktop.
Translate a postsecret's binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Translate binary into english characters. | |
# Written to translate a PostSecret on 01-24-2016. | |
# | |
# Author: Sam Liu <[email protected]> | |
# | |
# 01001001001000000110101101101110011011110111 | |
# 01110010000001001001001001110110110100100000 | |
# 01100001001000000110011101100101011001010110 | |
# 10110010111000100000010010010010011101101101 | |
# 001000000111000001110010011011110111010101100 | |
# 100001000000110111101100110001000000110100101 | |
# 110100001011100010000001000010011101010111010 | |
# 000100000011100110110111101101101011001010111 | |
# 010001101001011011010110010101110011001000000 | |
# 100100100100000011011010110100101110011011100 | |
# 110010000001100010011001010110100101101110011 | |
# 001110010000001100110011100100110100101100101 | |
# 011011100110010001110011001000000111011101101 | |
# 001011101000110100000100000011101000110100001 | |
# 1001010010000001100011011011110110111101101100 | |
# 001000000110101101101001011001000111001100101 | |
# 110 | |
import click | |
# For now, fix word size. | |
WORD_SIZE = 8 | |
@click.command() | |
@click.option('--filename', default='input.txt', help='File containing binary.') | |
@click.option('--word_size', default=8, help='Word size of the binary') | |
def translate_binary_file(filename, word_size): | |
f = open(filename) | |
# Convert input text into one long binary string. | |
binary_codes = "" | |
for line in f.readlines(): | |
binary_codes += line.strip() | |
# Split string into an array of WORD_SIZE binary bits. | |
binary_codes = [binary_codes[i:i + WORD_SIZE] | |
for i in range(0, len(binary_codes), WORD_SIZE)] | |
# Print each "word" (character). | |
for binary_code in binary_codes: | |
print chr(int(binary_code, 2)), | |
if __name__ == '__main__': | |
translate_binary_file() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment