我刚开始使用bash,我正在尝试将迅速混淆转换成bash脚本。
基本上,我想把一个字符串转换成一个无符号的-Int-8数组(UTF-8)。
例如,
"hey" = [104, 101, 121] (UTF-8 UINT8 value)
"example" = [101, 120, 97, 109, 112, 108, 101] (UTF-8 UINT8 value)有人知道这是否可能吗?
发布于 2022-07-18 16:12:22
使用纯bash,没有外部程序:
#!/usr/bin/env bash
to_codepoints() {
local LC_CTYPE=C IFS=, n
local -a cps
# Iterate over each byte of the argument and append its numeric value to an array
for (( n = 0; n < ${#1}; n++ )); do
cps+=( $(printf "%d" "'${1:n:1}") )
done
printf "[%s]\n" "${cps[*]}"
}
to_codepoints hey
to_codepoints example
to_codepoints $'\u00C4ccent'输出
[104,101,121]
[101,120,97,109,112,108,101]
[195,132,99,99,101,110,116]https://stackoverflow.com/questions/73019284
复制相似问题