How to parse long hexadecimal string into uint

后端 未结 1 968
粉色の甜心
粉色の甜心 2020-12-19 18:54

I\'m pulling in data that is in long hexadecimal string form which I need to convert into decimal notation, truncate 18 decimal places, and then serve up in JSON.

Fo

相关标签:
1条回答
  • 2020-12-19 19:25

    Use math/big for working with numbers larger than 64 bits.

    From the Int.SetString example:

    s := "d3c21bcecceda1000000"
    i := new(big.Int)
    i.SetString(s, 16)
    fmt.Println(i)
    

    https://play.golang.org/p/vf31ce93vA

    The math/big types also support the encoding.TextMarshaler and fmt.Scanner interfaces.

    For example

    i := new(big.Int)
    fmt.Sscan("0x000000d3c21bcecceda1000000", i)
    

    Or

    i := new(big.Int)
    fmt.Sscanf("0x000000d3c21bcecceda1000000", "0x%x", i)
    
    0 讨论(0)
提交回复
热议问题