How to get UTF8 from a hex variable?
With xxd
(usually shipped with vim
)
$ echo 5374c3a97068616e650a | xxd -p -r
Stéphane
If your locale's charset (see output of locale charmap
) is not UTF-8 (but can represent all the characters in the encoded string), add a | iconv -f UTF-8
.
If it cannot represent all the characters, you could try | iconv -f UTF-8 -t //TRANSLIT
to get an approximation.
Does Perl count?
$ echo "68656c6c6f0a" | \
perl -ne 'tr/a-fA-F0-9//cd; print pack("H*", $_)'
hello
If not, then maybe this might do:
$ echo "68656c6c6f0a" | sed -Ee 's/[0-9a-fA-F]{2}/\\\\x&/g' | xargs printf
hello
We need a literal backslash for printf
, but it's special for both xargs
and sed
so needs to be doubled twice. (\\\\x
-> \\x
-> \x
)