From: W. Trevor King Date: Wed, 24 Nov 2010 13:34:35 +0000 (-0500) Subject: Add ASCII hist post and ascii_hist.py script. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ed73463bca53f9829daa099e28c5106212e21d34;p=mw2txt.git Add ASCII hist post and ascii_hist.py script. --- diff --git a/posts/ASCII_hist.mdwn b/posts/ASCII_hist.mdwn new file mode 100644 index 0000000..0c6d9c2 --- /dev/null +++ b/posts/ASCII_hist.mdwn @@ -0,0 +1,7 @@ +[[ascii_hist.py]] is a very simple script to count the frequency of +each byte in a file. This is useful if you're playling around with +crypto. + +[[!tag tags/fun]] +[[!tag tags/programming]] +[[!tag tags/python]] diff --git a/posts/ASCII_hist/ascii_hist.py b/posts/ASCII_hist/ascii_hist.py new file mode 100755 index 0000000..57ffaa0 --- /dev/null +++ b/posts/ASCII_hist/ascii_hist.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# +# Copyright (C) 2010, William Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""Calculate ASCII character frequencies. +""" + +import sys + + +def read(stream): + hist = [0]*256 + while True: + data = stream.read(1024) + if len(data) == 0: + break + for char in data: + hist[ord(char)] += 1 + return hist + +def write(hist, stream=sys.stdout): + for char,count in enumerate(hist): + if count != 0: + print '%02x (%s)\t%d' % (char, chr(char), count) + + +if __name__ == '__main__': + try: + filename = sys.argv[1] + except IndexError: + filename = None + + if filename == None: + hist = read(sys.stdin) + else: + with open(filename, 'rb') as f: + hist = read(f) + + write(hist)