Search code examples
hexascii

Bash. Hex to ascii. Is possible without xxd or perl?


I'm developing a script on which I have a hex string 31323334353637383930313233 and I want to transform it into ASCII. Desired output is 1234567890123.

I already have it working using:

echo "31323334353637383930313233" | xxd -r -p

or

echo "31323334353637383930313233" | perl -pe 's/(..)/chr(hex($1))/ge'

But the point is try to use the minimum possible requirements for the script. I want it working in suse, fedora, debian, ubuntu, arch, etc... It seems the xxd command is included in vim package. I'm wondering if there is a way to achieve this using only awk or any internal Linux tool which is going to be present by default in all Linux systems.


Solution

  • Alternate (g)awk solution:

    echo "31323334353637383930313233" | awk 'RT{printf "%c", strtonum("0x"RT)}' RS='[0-9]{2}'