Bash十进制到基数62的转换 [英] Bash decimal to base 62 conversion
问题描述
我想撤消以下 bash
命令执行的操作:
I would like to reverse the operation performed by the following bash
command:
$ echo $((62#a39qrT))
9207903953
即将十进制9207903953 转换为 base 62 ,并保持 bash
的标准为 {0..9},{a..z},{A..Z}
.
i.e. convert decimal 9207903953 to base 62, keeping bash
standard of {0..9},{a..z},{A..Z}
.
我知道我可以使用 bc
来做到这一点,但是那时我将必须手动转换每个字符.例如,我目前正在这样做:
I know I can do this by using bc
, but I will have to manually convert each character then. For example, I do this currently:
BASE62=($(echo {0..9} {a..z} {A..Z}))
for i in $(echo "obase=62; 9207903953" | bc)
do
echo -n ${BASE62[$i]} #Doesn't work if bc's output contains leading zeroes
done
必须有一种减少黑客"行为的方法.您知道更有效地做到这一点的方法吗?
There must be a way to do this in a less 'hackier' way. Do you know of a way to do this more efficiently?
更改了 bc
输入.
推荐答案
我真的很感谢您提出的解决方案,我想bash无法解决它.这是您错过的一点:
I do really appreciate the solution you came up with, and I guess there's no way around it straight with bash. Here's the little point you've missed:
BASE62=($(echo {0..9} {a..z} {A..Z}))
for i in $(bc <<< "obase=62; 9207903953"); do
echo -n ${BASE62[$(( 10#$i ))]}
done && echo
输出:
a39qrT
这篇关于Bash十进制到基数62的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!