duanping3587 2013-10-06 21:36
浏览 24
已采纳

访问未导出的函数“ not.string”

I want to convert a big.Int to simple base32. Not the standard base32 stuff like the RFC 4648 implemented by base32 nor zBase32 nor Crockford I want just simple normal 5-bits per character 0-9A-V character set.

I am aware of the base32 package and it does not do what I want - it builds the result in a standard base 32 number with padding and stuff I don't want. Certainly I could use it and tear off the trailing "=" characters and hack what remains but that just seems like a brutal solution.

There is a big.SetString(string, base) that can parse a base32 number in string form but there is no reverse - which is what I am really looking for, a big.GetString(base) like the Java BigInteger.toString(int base).

There is, however, a nat.string which does exactly what I want. How can I gain access to it?

Is there a way I could manually extend big to implement big.GetString(base) which trivially calls nat.string with the correct charset?

Is there a way I can reach into the unexported big.nat which big uses and call nat.string?

Is there something else I can do?

P.S. I'd also be interested in using nat.trailingZeroBits - I had to write my own because I didn't realise this was already done.

  • 写回答

3条回答 默认 最新

  • dovgqc2648 2013-10-07 02:47
    关注

    You can't access unexported functions at all. You'd need to re-write at least some portion of nat.go in order to achieve that functionality. Some of those functions look very useful, so it may be worth sending a feature request to the golang-nuts group asking for some of them to be exported in a future release.

    You can however use strconv.FormatInt() to do what you require.

    given a big.Int b you can do:

    strconv.FormatInt(b.Int64(), 32)
    

    Full example:

    package main
    
    import (
        "fmt"
        "math/big"
        "strconv"
    )
    
    func main() {
        i := 3286583923486565782 // Some random integer
        b := big.NewInt(int64(i))
        fmt.Println(strconv.FormatInt(b.Int64(), 32))
    }
    

    Produces:

    2r72al99uq9cm

    Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据