download201401 2017-11-15 19:25 采纳率: 0%
浏览 28
已采纳

如何将长十六进制字符串解析为uint

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.

For example I may have the hex string:

"0x00000000000000000000000000000000000000000000d3c21bcecceda1000000"

At first I was attempting to use ParseUint(), however since the highest it supports is int64, my number ends up being way too big.

This example after conversion and truncation results in 10^6. However there are instances where this number can be up to 10^12 (meaning pre truncation 10^30!).

What is the best strategy to attack this?

  • 写回答

1条回答 默认 最新

  • duanfuxing2212 2017-11-15 19:34
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么