Welcome, guest | Sign In | My Account | Store | Cart

There are lots of hex editors out there, but how difficult is it to actually write a program that just displays a file in simple hex format? As this recipe shows, a few functions and lines in Python makes it rather easy to accomplish.

Python, 23 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import sys

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

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()