Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 421
Wiliam Carvalho Avatar asked Jun 17 '26 04:06

Wiliam Carvalho


1 Answers

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
like image 70
tshiono Avatar answered Jun 20 '26 12:06

tshiono