Convert string to list of ASCII codes in bash

If you want to take a string and get the ascii code for each character in the string using a bash script then use the following function:

function string2ascii_codes {
str=$1;
list=($(echo $str|sed 's/\(.\)/\1 /g'));
ascii_result='';
for element in "${list[@]}"
do
ascii_result+=$(printf "%d," "'$element");
done
echo "${ascii_result%?}";
}

The following is an example of how the function above is used:

string2ascii_codes "ABCDEF";

This prints out the result:

65,66,67,68,69,70

comments powered by Disqus