Last active
December 9, 2022 03:46
-
-
Save dstebila/6194f90097c43c091dd2 to your computer and use it in GitHub Desktop.
Shell script to convert hex file to binary, stripping out any comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "0x" prefixes | |
perl -0777 -pe 's{\b0x(..)\b}{$1}gi' | \ | |
# 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
Good suggestion, I've updated it.