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
30
31
32
33
34
35
|
#!/usr/bin/env python3
import argparse
import sys
from functools import partial
parser = argparse.ArgumentParser(description='Convert a file to C array')
parser.add_argument('input', type=str, help='Input file')
parser.add_argument('--output', type=argparse.FileType('wt'), default=sys.stdout, help='Output file')
parser.add_argument('--array-type', type=str, default='const unsigned char', help='Array type')
parser.add_argument('--length-type', type=str, default='const unsigned int', help='Length type')
parser.add_argument('--name', type=str, default='a', help='Array name')
args=parser.parse_args()
print("{} {}[] = {{".format(args.array_type, args.name), file=args.output)
n = 0
with open(args.input, "rb") as in_file:
for c in iter(partial(in_file.read, 1), b''):
if n % 16 == 0:
print(" ", end='', file=args.output)
print("0x%02X," % ord(c), end='', file=args.output)
n += 1
if n % 16 == 0:
print("", file=args.output)
else:
print(" ", end='', file=args.output)
print("\n};", file=args.output)
print("{} {}_len = {};".format(args.length_type, args.name, n), file=args.output)
|