Welcome, guest | Sign In | My Account | Store | Cart
import os
import sys

PRINT = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc\
defghijklmnopqrstuvwxyz{|}~'

def main():
    try:
        data = open(sys.argv[1], 'rb')
        for number in xrange(0, os.path.getsize(sys.argv[1]), 16):
            part = data.read(16)
            sys.stdout.write('%08X | %s | %s\n' % (number,
                                                   hexit(part).ljust(47),
                                                   clean(part)))
        data.close()
    except:
        sys.stdout.write('Usage: %s <filename>' % os.path.basename(sys.argv[0]))

def hexit(string):
    return ' '.join('%02X' % ord(c) for c in string)

def clean(string):
    return ''.join(c in PRINT and c or '.' for c in string)

if __name__ == '__main__':
    main()

Diff to Previous Revision

--- revision 1 2007-02-01 21:46:37
+++ revision 2 2012-07-04 01:07:49
@@ -1,17 +1,20 @@
 import os
 import sys
 
-PRINT = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
+PRINT = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc\
+defghijklmnopqrstuvwxyz{|}~'
 
 def main():
     try:
-        show(file(sys.argv[1], 'rb').read())
+        data = open(sys.argv[1], 'rb')
+        for number in xrange(0, os.path.getsize(sys.argv[1]), 16):
+            part = data.read(16)
+            sys.stdout.write('%08X | %s | %s\n' % (number,
+                                                   hexit(part).ljust(47),
+                                                   clean(part)))
+        data.close()
     except:
-        sys.stdout.write(os.path.basename(sys.argv[0]) + ' <filename>')
-
-def show(string):
-    for number, part in enumerate(string[index:index+16] for index in xrange(0, len(string), 16)):
-        sys.stdout.write('%07X0 | %s| %s\n' % (number, hexit(part).ljust(48), clean(part)))
+        sys.stdout.write('Usage: %s <filename>' % os.path.basename(sys.argv[0]))
 
 def hexit(string):
     return ' '.join('%02X' % ord(c) for c in string)

History