Browse Source

tools/hexdump: make --output work

master
Aqua-sama 7 months ago
parent
commit
f1501b3733
Signed by: Aqua-sama <aqua@iserlohn-fortress.net> GPG Key ID: 5378B8349C1D5ADA
1 changed files with 9 additions and 8 deletions
  1. 9
    8
      tools/hexdump.py

+ 9
- 8
tools/hexdump.py View File

@@ -1,34 +1,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=str, help='Output 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))
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='')
print(" ", end='', file=args.output)

print("0x%02X," % ord(c), end='')
print("0x%02X," % ord(c), end='', file=args.output)

n += 1
if n % 16 == 0:
print("")
print("", file=args.output)
else:
print(" ", end='')
print(" ", end='', file=args.output)

print("\n};")
print("{} {}_len = {};".format(args.length_type, args.name, n))
print("\n};", file=args.output)
print("{} {}_len = {};".format(args.length_type, args.name, n), file=args.output)


Loading…
Cancel
Save