myNumber=$(date +%s) # big number in decimal
myNumberInB58=$(toBase58 $myNumber)
toBase58() {
# <your answer here>
}
在Base58编码整数的最优雅和/或最简洁的方法是什么?
答案 0 :(得分:3)
bitcoin-bash-tools提供函数{en,de}codeBase58
:
decodeBase58() {
echo -n "$1" | sed -e's/^\(1*\).*/\1/' -e's/1/00/g' | tr -d '\n'
dc -e "$dcr 16o0$(sed 's/./ 58*l&+/g' <<<$1)p" |
while read n; do echo -n ${n/\\/}; done
}
encodeBase58() {
echo -n "$1" | sed -e's/^\(\(00\)*\).*/\1/' -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
。
答案 1 :(得分:0)
这会吗?
a=( {1..9} {A..H} {J..N} {P..Z} {a..k} {m..z} )
toBase58() {
# TODO: check that $1 is a valid number
local nb=$1 b58= fiftyeight=${#a[@]}
while ((nb)); do
b58=${a[nb%fiftyeight]}$b58
((nb/=fiftyeight))
done
printf '%s\n' "$b58"
}