I am new to bash and I am trying to convert a swift obfuscation into a bash script.
Basically, I want to convert a string into an Unsigned-Int 8 array (UTF-8).
For example,
"hey" = [104, 101, 121] (UTF-8 UINT8 value)
"example" = [101, 120, 97, 109, 112, 108, 101] (UTF-8 UINT8 value)
Does anyone know if this is possible?
The following shell script converts input in the for of hey
into the string [104, 101, 121]
.
# Print hey
printf "%s" hey |
# convert to hex one per line
xxd -p -c 1 |
# convert to decimal one per line
xargs -I{} printf "%d\n" 0x{} |
# Join lines with comma
paste -sd, |
# Add spaces after comma
sed 's/,/, /g' |
# Add [ ]
{ echo -n '['; tr -d '\n'; echo ']'; }
# echo "[$(cat)]"
The script is not aware of input encoding - the script only translates bytes representation. The input string has to be already in the desired encoding. Use iconv
to convert between encodings.
edit: it looks pretty inefficient, I would do:
$ printf "%s" hey | xxd -p -c 1 | { printf "["; xargs -I{} printf "%d, " 0x{} | sed 's/, $//'; printf "]\n"; }
[104, 101, 121]
Or in pure bash should be even faster:
string=hey
output=""
for ((i=0;i<${#string};i++)); do
printf -v output "%s%d" "$output${output:+, }" "'${string:i:1}"
done
echo "[$output]";
# outputs: [104, 101, 121]