Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell script to convert hex file to binary, stripping out any comments
#!/bin/bash
# Read either the first argument or from stdin (http://stackoverflow.com/questions/6980090/bash-read-from-file-or-stdin)
cat "${1:-/dev/stdin}" | \
# Strip out comments starting with #
sed -E 's/#.*$//' | \
# Strip out comments starting with //
sed -E 's/\/\/.*$//' | \
# Strip out multi-line comments /* ... */
perl -0777 -pe 's{/\*.*?\*/}{}gs' | \
# Strip out all non hexadecimal characters
sed -E 's/[^0-9a-fA-F]*//g' | \
# Convert hex to binary using xxd's reverser in plain hexdump style
xxd -r -ps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.