Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | import os.path |
| 3 | import string |
| 4 | import sys |
| 5 | |
| 6 | |
| 7 | def print_buf(counter, buf): |
| 8 | buf2 = [('%02x' % ord(i)) for i in buf] |
| 9 | print '{0}: {1:<39} {2}'.format(('%07x' % (counter * 16)), |
| 10 | ' '.join([''.join(buf2[i:i + 2]) for i in range(0, len(buf2), 2)]), |
| 11 | ''.join([c if c in string.printable[:-5] else '.' for c in buf])) |
| 12 | |
| 13 | |
| 14 | def process_xxd(file_path): |
| 15 | with open(file_path, 'r') as f: |
| 16 | counter = 0 |
| 17 | while True: |
| 18 | buf = f.read(16) |
| 19 | if not buf: |
| 20 | break |
| 21 | print_buf(counter, buf) |
| 22 | counter += 1 |
| 23 | |
| 24 | |
| 25 | if __name__ == '__main__': |
| 26 | if not os.path.exists(sys.argv[1]): |
| 27 | print >> (sys.stderr, "The file doesn't exist.") |
| 28 | sys.exit(1) |
| 29 | process_xxd(sys.argv[1]) |