Shell Scipt将位字符串转换为文本值代码

我有32位0字符串。字符串中的每一位代表一些代码,例如

1000000000000000000000000000000  = ABC
0100000000000000000000000000000 = DEF
...
0000000000000000000000000000001 = XYZ

我有字符串位的文件,有什么办法可以将输出的shell脚本编写为代码 对于。例如

1100000000000000000000000000000  should print ABC|DEF

感谢您的输入。

edit1: @nullPointer:3位字母是人类可读的32位表示形式的示例。

yangshenghan521 回答:Shell Scipt将位字符串转换为文本值代码

bash中:

codes=("ABC" "DEF" ... "XYZ")
bitstring=1100000000000000000000000000000

outstring=""
for i in {0..31}; do
    if [ "${bitstring:i:1}" = 1 ]; then
        outstring+="|${codes[i]}"
    fi
done
echo "${outstring#|}"
本文链接:https://www.f2er.com/3009275.html

大家都在问