Skip to content

Instantly share code, notes, and snippets.

@tcely
Last active May 14, 2022 17:15
Show Gist options
  • Save tcely/e86ebe7c86b6e4c2d8a1 to your computer and use it in GitHub Desktop.
Save tcely/e86ebe7c86b6e4c2d8a1 to your computer and use it in GitHub Desktop.
Bash functions for converting hexadecimal strings back to ASCII and for converting ASCII strings to hexadecimal strings.
#!/bin/bash
#
hextostr() {
local i _hs="$*"
local _hsl="${#_hs}"
printf '%s\n' "$(for ((i=0; i < _hsl; i+=2)); do echo -ne "\x${_hs:i:2}"; done)"
unset -v _hsl _hs i
}
#hextostr '48656C6C6F20776F726C6421'
#Hello world!
strtohex() {
local i _as="$*"
local _asl="${#_as}"
for ((i=0; i < _asl; i++))
do
printf '%02x' "'${_as:i:1}"
done
echo
unset -v _asl _as i
}
#strtohex 'Hello world!'
#48656c6c6f20776f726c6421
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment