Issue
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?
Solution
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.
Answered By - KamilCuk Answer Checked By - Senaida (WPSolving Volunteer)