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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?