将 []uint8 转换为 float64。

huangapple go评论85阅读模式
英文:

Converting []uint8 to float64

问题

处理格式为[]uint8而不是JSON的http resp.Body的最佳方法是什么?
我想将字节转换为float64

这是返回的值:

value : %!F([]uint8=[48 46 48 48 49 50 53 53 50 49])
英文:

What is the best way to handle an http resp.Body which is formatted as []uint8 and not as JSON?
I would like to convert the bytes into a float64.

This is the returned value response:

value : %!F([]uint8=[48 46 48 48 49 50 53 53 50 49])

答案1

得分: 5

尝试使用strconv包中的ParseFloat函数(play):

b := []uint8{48, 46, 48, 48, 49, 50, 53, 53, 50, 49}

f, err := strconv.ParseFloat(string(b), 64)

if err != nil {
    // 处理解析错误
}

fmt.Printf("%f\n", f) // 0.001255
英文:

Try using ParseFloat from the strconv package (play):

b := []uint8{48, 46, 48, 48, 49, 50, 53, 53, 50, 49}

f, err := strconv.ParseFloat(string(b), 64)

if err != nil { 
    // Handle parse error
}

fmt.Printf("%f\n", f) // 0.001255

huangapple
  • 本文由 发表于 2014年2月6日 09:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/21592555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定