dpp3047 2019-06-06 12:09
浏览 37

严格将字符串转换为int [关闭]

I am trying to store a hexadecimal string value : ex "3958ABBFEC23BD40" to uint64 which would like this:

fmt.Println(myuint64)

$ 0x3958ABBFEC23BD40

I have tried using encoding/hex, or strconv to int, etc... I couldn't find any solution.

file, err := os.Open("myfile")
if err != nil {
    fmt.Println("erreur",err)
}
b := bufio.NewReader(file)
a,_ := b.Peek(16)
d, err := strconv.ParseUint(string(a), 0, 64)
if err != nil {
    panic(err)
}
fmt.Printf("%d
%s
", d,string(a))

Obviously I am not getting the result expected :

$ 4132241498901298496
fcb9ca7c63ec7c5d

expected :

$ 0xfcb9ca7c63ec7c5d

using the following code, I have retrieve the good uint, but I can't convert it to hexa, unless by using strconv, but I don't wish to work with a string.

v := *(*[8]byte)(unsafe.Pointer(&d))
var final []byte = v[:]
var n uint64
buf := bytes.NewBuffer(final)
binary.Read(buf, binary.LittleEndian, &n)
fmt.Println(strconv.FormatUint(n, 16))

result : $ fcb9ca7c63ec7c5d but as string, and expected $ fcb9ca7c63ec7c5d type of uint64...

  • 写回答

1条回答 默认 最新

  • drpsrvu85668 2019-06-06 13:25
    关注

    You need to use strconv.ParseUint.

    d, err := strconv.ParseUint("0x3958ABBFEC23BD40", 0, 64)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d
    ", d)
    

    You can try this code out on the Go Playground.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向