【问题标题】:How to convert decimal number to Base58 in Bash如何在 Bash 中将十进制数转换为 Base58
【发布时间】:2015-06-03 21:12:31
【问题描述】:
myNumber=$(date +%s) # big number in decimal
myNumberInB58=$(toBase58 $myNumber)

toBase58() {
  # <your answer here>
}

Base58 中编码整数的最优雅和/或简洁的方法是什么?

【问题讨论】:

标签: bash base-conversion


【解决方案1】:

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
}

这些与文件中直接在上面定义的字段dcrbase58 一起使用。

【讨论】:

    【解决方案2】:

    这样可以吗?

    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"
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-04
      • 2013-05-07
      • 1970-01-01
      • 2016-05-21
      • 2017-09-04
      • 1970-01-01
      • 2015-09-23
      • 2019-05-10
      • 2016-03-08
      相关资源
      最近更新 更多