如何在 Bash 中将十进制数转换为 Base58
How to convert decimal number to Base58 in Bash
myNumber=$(date +%s) # big number in decimal
myNumberInB58=$(toBase58 $myNumber)
toBase58() {
# <your answer here>
}
在Base58中编码整数最优雅and/or的简洁方法是什么?
这样可以吗?
a=( {1..9} {A..H} {J..N} {P..Z} {a..k} {m..z} )
toBase58() {
# TODO: check that is a valid number
local nb= b58= fiftyeight=${#a[@]}
while ((nb)); do
b58=${a[nb%fiftyeight]}$b58
((nb/=fiftyeight))
done
printf '%s\n' "$b58"
}
bitcoin-bash-tools 提供函数 {en,de}codeBase58
:
decodeBase58() {
echo -n "" | sed -e's/^\(1*\).*//' -e's/1/00/g' | tr -d '\n'
dc -e "$dcr 16o0$(sed 's/./ 58*l&+/g' <<<)p" |
while read n; do echo -n ${n/\/}; done
}
encodeBase58() {
echo -n "" | sed -e's/^\(\(00\)*\).*//' -e's/00/1/g' | tr -d '\n'
dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" |
while read -r n; do echo -n "${base58[n]}"; done
}
那些与直接在上面文件中定义的字段 dcr
和 base58
一起工作。
myNumber=$(date +%s) # big number in decimal
myNumberInB58=$(toBase58 $myNumber)
toBase58() {
# <your answer here>
}
在Base58中编码整数最优雅and/or的简洁方法是什么?
这样可以吗?
a=( {1..9} {A..H} {J..N} {P..Z} {a..k} {m..z} )
toBase58() {
# TODO: check that is a valid number
local nb= b58= fiftyeight=${#a[@]}
while ((nb)); do
b58=${a[nb%fiftyeight]}$b58
((nb/=fiftyeight))
done
printf '%s\n' "$b58"
}
bitcoin-bash-tools 提供函数 {en,de}codeBase58
:
decodeBase58() {
echo -n "" | sed -e's/^\(1*\).*//' -e's/1/00/g' | tr -d '\n'
dc -e "$dcr 16o0$(sed 's/./ 58*l&+/g' <<<)p" |
while read n; do echo -n ${n/\/}; done
}
encodeBase58() {
echo -n "" | sed -e's/^\(\(00\)*\).*//' -e's/00/1/g' | tr -d '\n'
dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" |
while read -r n; do echo -n "${base58[n]}"; done
}
那些与直接在上面文件中定义的字段 dcr
和 base58
一起工作。