aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2019-03-22 17:08:28 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2019-03-22 17:08:28 +0200
commitf1501b3733d29805b0743ac02929bdd8f3c3454d (patch)
treecd4d7e3077341e94a0ba1bdbfebfd26141ad40df
parentReplace xxd with python script (diff)
downloadsmolbote-f1501b3733d29805b0743ac02929bdd8f3c3454d.tar.xz
tools/hexdump: make --output work
-rwxr-xr-xtools/hexdump.py17
1 files changed, 9 insertions, 8 deletions
diff --git a/tools/hexdump.py b/tools/hexdump.py
index f79fc5f..c0ed2a3 100755
--- a/tools/hexdump.py
+++ b/tools/hexdump.py
@@ -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)