Skip to content

Commit

Permalink
add hexdump.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmoon79 committed Jul 11, 2022
1 parent 68ee650 commit 031434f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tools/hexdump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
#
# quick Python script to dump a file to hex format that can be copy+pasted into
# a rust `[u8]` array
# believe it or not, I couldn't get `hexdump` or `xxd` or `od` to dump binary
# data in the format I wanted

import sys


WIDTH = 8


def main(path: str, width: int):
with open(path, "rb") as file_:
data = file_.read()
for at, byte_ in enumerate(data):
print(f"0x{byte_:02x},", end="")
if (at + 1) % width == 0:
print("")
else:
print(" ", end="")


if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage:\n hexdump.py FILE [WIDTH]", file=sys.stderr)
sys.exit(1)
path = sys.argv[1]
width = WIDTH
if len(sys.argv) == 3:
width = int(sys.argv[2])
main(path, width)

0 comments on commit 031434f

Please sign in to comment.