Skip to content

Instantly share code, notes, and snippets.

@maddouri
Last active October 11, 2018 21:30
Show Gist options
  • Save maddouri/017a68f3d01f76f164f80fce99b79151 to your computer and use it in GitHub Desktop.
Save maddouri/017a68f3d01f76f164f80fce99b79151 to your computer and use it in GitHub Desktop.
Fun with binary/hex data
# https://stackoverflow.com/a/2615112/865719
# https://stackoverflow.com/a/7826789/865719
# https://stackoverflow.com/q/2764051/865719
# echo -n 'Hello' | tohex
# tohex INPUT_FILE
function tohex() { hexdump -ve '1/1 "%.2x"' "$@" ; }
function todec() { hexdump -ve '1/1 "%.3d"' "$@" ; }
function tobin() {
xxd -b "$@" | cut -d" " -f2-7 | tr -d ' ' | tr -d '\n'
}
# fromhex INPUT_FILE [OUTPUT_FILE]
# echo -n 'Hello' | tohex | fromhex [OUTPUT_FILE]
function fromhex() {
if [ -t 0 ] ; then # not piping
xxd -r -p "$@"
else
xxd -r -p - "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment