How to get UTF8 from a hex variable?
I’m debugging an app for a client and I found the information from the DB which could be solution. I ask the client to extract it but unfortunately the client sent me the raw data in hexadecimal…
I ask the client to resend me the plain text from the DB tools but awaiting their response I’m looking for a bash solution.
I know the encoded data is a UTF-8 encoded string: is there a way to decode it with Unix tools?
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
)
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.