Last active
May 14, 2022 17:15
-
-
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.
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 | |
# | |
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