blob: b81e85490116bc81762f04a57e160a7d9ea4c2e1 [file] [log] [blame]
Ed Tanousf9273472017-02-28 16:05:13 -08001# -*- coding: utf-8 -*-
2import os.path
3import string
4import sys
5
6
7def 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
14def 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
25if __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])