From f9e85b7ddddd3b6a87dfc4d907cb1fbe16e22e77 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 22 Mar 2019 16:48:12 +0200 Subject: Replace xxd with python script --- tools/hexdump.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 tools/hexdump.py (limited to 'tools/hexdump.py') diff --git a/tools/hexdump.py b/tools/hexdump.py new file mode 100755 index 0000000..f79fc5f --- /dev/null +++ b/tools/hexdump.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import argparse +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('--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)) + +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("0x%02X," % ord(c), end='') + + n += 1 + if n % 16 == 0: + print("") + else: + print(" ", end='') + +print("\n};") +print("{} {}_len = {};".format(args.length_type, args.name, n)) + -- cgit v1.2.1