bash ascii to hex bash ascii to hex bash bash

bash ascii to hex


$ str="hello"$ hex=$(xxd -pu <<< "$str")$ echo "$hex"6C6C6568A6F

Or:

$ hex=$(hexdump -e '"%X"' <<< "$str")$ echo "$hex"6C6C6568A6F

Careful with the '"%X"'; it has both single quotes and double quotes.


You have several options

$ printf hello | xxd0000000: 6865 6c6c 6f                             hello

See also:Ascii/Hex convert in bash


here's a one liner (a little complex but works fine):

#!/bin/bashecho '0x'"`echo $1 | hexdump -vC |  awk 'BEGIN {IFS="\t"} {$1=""; print }' | awk '{sub(/\|.*/,"")}1'  | tr -d '\n' | tr -d ' '`" | rev | cut -c 3- | rev