Search code examples
linuxbashhexdump

how to "decdump" a string in bash?


I need to convert a string into a sequence of decimal ascii code using bash command.

example: for the string 'abc' the desired output would be 979899
where a=97, b=98 and c=99 in ascii decimal code.

I was able to achieve this with ascii hex code using xxd.

printf '%s' 'abc' | xxd -p

which gives me the result: 616263
where a=61, b=62 and c=63 in ascii hexadecimal code.

Is there an equivalent to xxd that gives the result in ascii decimal code instead of ascii hex code?


Solution

  • If you don't mind the results are merged into a line, please try the following:

    echo -n "abc" | xxd -p -c 1 |
    while read -r line; do
        echo -n "$(( 16#$line ))"
    done
    

    Result:

    979899