Last active
October 11, 2018 21:30
-
-
Save maddouri/017a68f3d01f76f164f80fce99b79151 to your computer and use it in GitHub Desktop.
Fun with binary/hex data
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
# 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