I found this function at https://raw.github.com/lhunath/scripts/master/bash/bashlib/bashlib :
hex() { printf '%x' "'$1" }
Other examples:
$ printf %x\\n \'ใข7fc2 $ LC_CTYPE=C printf %x\\n \'ใขe3 $ printf %s $' \n\n\\'|while IFS= read -r -d '' -n1 c;do printf %x\\n "'$c";done 20 20 a a 5c $ printf %s aรคใข๐|while IFS= read -r -d '' -n1 c;do printf '%s %x\n' "$c" "'$c";done a 61 รค e4ใข 30a2 ๐ 1d400
This worked with the built-in printf in bash 4.2 and zsh 4.3.11, but not with the built-in printf in bash 3.2 or with OS X /usr/bin/printf .
-n1 reads one character at a time, and -d '' changes the delimiter from \n to \0 , so read also includes strings (but not NUL characters). The only read option given by POSIX is -r .
source share