summaryrefslogtreecommitdiff
path: root/scripts/file_to_string_array.py
blob: b81e85490116bc81762f04a57e160a7d9ea4c2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
import os.path
import string
import sys


def print_buf(counter, buf):
    buf2 = [('%02x' % ord(i)) for i in buf]
    print '{0}: {1:<39}  {2}'.format(('%07x' % (counter * 16)),
        ' '.join([''.join(buf2[i:i + 2]) for i in range(0, len(buf2), 2)]),
        ''.join([c if c in string.printable[:-5] else '.' for c in buf]))


def process_xxd(file_path):
    with open(file_path, 'r') as f:
        counter = 0
        while True:
            buf = f.read(16)
            if not buf:
                break
            print_buf(counter, buf)
            counter += 1


if __name__ == '__main__':
    if not os.path.exists(sys.argv[1]):
        print >> (sys.stderr, "The file doesn't exist.")
        sys.exit(1)
    process_xxd(sys.argv[1])