aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2019-03-22 16:48:12 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2019-03-22 16:48:12 +0200
commitf9e85b7ddddd3b6a87dfc4d907cb1fbe16e22e77 (patch)
treec5e30ac4b1b0103ac00918f04c729fefb97f89d8 /tools
parentAdd PluginLoader class (diff)
downloadsmolbote-f9e85b7ddddd3b6a87dfc4d907cb1fbe16e22e77.tar.xz
Replace xxd with python script
Diffstat (limited to 'tools')
-rwxr-xr-xtools/hexdump.py34
1 files changed, 34 insertions, 0 deletions
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))
+