将字节切片”[]uint8″转换为Go语言中的float64。

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

Convert byte slice "[]uint8" to float64 in GoLang

问题

我正在尝试将一个[]uint8字节切片转换为Go语言中的float64类型。我在网上找不到解决这个问题的方法。我看到有建议先转换为字符串,然后再转换为float64,但这似乎不起作用,它会丢失其值,最终得到的是零。

示例:

  1. metric.Value, _ = strconv.ParseFloat(string(column.Value), 64)

但这并不起作用...

英文:

I'm trying to convert a []uint8 byte slice into a float64 in GoLang. I can't find a solution for this issue online. I've seen suggestions of converting to a string first and then to a float64 but this doesn't seem to work, it loses it's value and I end up with zeroes.

Example:

  1. metric.Value, _ = strconv.ParseFloat(string(column.Value), 64)

And it doesn't work...

答案1

得分: 67

例如,

  1. package main
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math"
  6. )
  7. func Float64frombytes(bytes []byte) float64 {
  8. bits := binary.LittleEndian.Uint64(bytes)
  9. float := math.Float64frombits(bits)
  10. return float
  11. }
  12. func Float64bytes(float float64) []byte {
  13. bits := math.Float64bits(float)
  14. bytes := make([]byte, 8)
  15. binary.LittleEndian.PutUint64(bytes, bits)
  16. return bytes
  17. }
  18. func main() {
  19. bytes := Float64bytes(math.Pi)
  20. fmt.Println(bytes)
  21. float := Float64frombytes(bytes)
  22. fmt.Println(float)
  23. }

输出:

  1. [24 45 68 84 251 33 9 64]
  2. 3.141592653589793
英文:

For example,

  1. package main
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math"
  6. )
  7. func Float64frombytes(bytes []byte) float64 {
  8. bits := binary.LittleEndian.Uint64(bytes)
  9. float := math.Float64frombits(bits)
  10. return float
  11. }
  12. func Float64bytes(float float64) []byte {
  13. bits := math.Float64bits(float)
  14. bytes := make([]byte, 8)
  15. binary.LittleEndian.PutUint64(bytes, bits)
  16. return bytes
  17. }
  18. func main() {
  19. bytes := Float64bytes(math.Pi)
  20. fmt.Println(bytes)
  21. float := Float64frombytes(bytes)
  22. fmt.Println(float)
  23. }

Output:

  1. [24 45 68 84 251 33 9 64]
  2. 3.141592653589793

答案2

得分: 6

我认为你正在寻找的是Go文档中的这个示例:
http://golang.org/pkg/encoding/binary/#example_Read

  1. var pi float64
  2. b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
  3. buf := bytes.NewReader(b)
  4. err := binary.Read(buf, binary.LittleEndian, &pi)
  5. if err != nil {
  6. fmt.Println("binary.Read failed:", err)
  7. }
  8. fmt.Print(pi)

打印结果为3.141592653589793。

英文:

I think this example from Go documentation is what you are looking for:
http://golang.org/pkg/encoding/binary/#example_Read

  1. var pi float64
  2. b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
  3. buf := bytes.NewReader(b)
  4. err := binary.Read(buf, binary.LittleEndian, &pi)
  5. if err != nil {
  6. fmt.Println("binary.Read failed:", err)
  7. }
  8. fmt.Print(pi)

Prints 3.141592653589793

答案3

得分: 6

根据评论所说,一切取决于你的[]uint8切片中包含的是什么类型的数据。

如果它是表示IEEE 754浮点数值的字节,采用Kluyg或peterSo的答案(后者性能更好,不使用反射)。

如果它是Latin-1/UTF-8编码的文本表示,则你应该能够像你刚才做的那样操作:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. var f float64
  8. text := []uint8("1.23") // 以Latin-1文本表示的十进制值
  9. f, err := strconv.ParseFloat(string(text), 64)
  10. if err != nil {
  11. panic(err)
  12. }
  13. fmt.Println(f)
  14. }

结果:

1.23

Playground: http://play.golang.org/p/-7iKRDG_ZM

英文:

As the comments read, it all depends on what kind of data you have in your []uint8 slice.

If it is bytes representing an IEEE 754 floating-point value in Little Endian order, then use Kluyg's or peterSo's (better performance without use of reflection) answer.

If it is a textual representation in Latin-1/UTF-8 encoding, then you should be able to do what you just did:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. var f float64
  8. text := []uint8("1.23") // A decimal value represented as Latin-1 text
  9. f, err := strconv.ParseFloat(string(text), 64)
  10. if err != nil {
  11. panic(err)
  12. }
  13. fmt.Println(f)
  14. }

Result:

>1.23

Playground: http://play.golang.org/p/-7iKRDG_ZM

答案4

得分: 2

我希望这个技巧能有所帮助。它的目的是将一长串二进制数转换为浮点数。

例如:
0110111100010010100000111100000011001010001000010000100111000000 -> -3.1415

  1. func binFloat(bin string) float64 {
  2. var s1 []byte
  3. var result float64
  4. if len(bin) % 8 == 0 {
  5. for i := 0; i < len(bin) / 8; i++ {
  6. // 将字符串分割成长度为8的片段
  7. // 将字符串转换为整数,再转换为字节
  8. num, _ := strconv.ParseInt(bin[8*i: 8*(i + 1)], 2, 64)
  9. // 将字节存入切片s1
  10. s1 = append(s1, byte(num))
  11. }
  12. }
  13. // 将字节切片转换为float64类型
  14. // 下面的算法来自于golang二进制示例
  15. buf := bytes.NewReader(s1)
  16. // 你也可以将binary.LittleEndian改为binary.BigEndian
  17. // 有关字节序的详细信息,请搜索字节序
  18. err := binary.Read(buf, binary.LittleEndian, &result)
  19. if err != nil {
  20. panic(err)
  21. fmt.Println("二进制长度不是8的倍数")
  22. }
  23. return result
  24. }
英文:

I hope this hack helps. The purpose of it is to convert the long stream of binary numbers to float.

For example:
0110111100010010100000111100000011001010001000010000100111000000 -> -3.1415

  1. func binFloat(bin string) float64 {
  2. var s1 []byte
  3. var result float64
  4. if len(bin) % 8 == 0 {
  5. for i := 0; i &lt; len(bin) / 8; i++ {
  6. //Chop the strings into a segment with a length of 8.
  7. //Convert the string to Integer and to byte
  8. num, _ := strconv.ParseInt(bin[8*i: 8*(i + 1)], 2, 64)
  9. //Store the byte into a slice s1
  10. s1 = append(s1, byte(num))
  11. }
  12. }
  13. //convert the byte slice to a float64.
  14. //The algorithm below are copied from golang binary examples.
  15. buf := bytes.NewReader(s1)
  16. //You can also change binary.LittleEndian to binary.BigEndian
  17. //For the details of Endianness, please google Endianness
  18. err := binary.Read(buf, binary.LittleEndian, &amp;result)
  19. if err != nil {
  20. panic(err)
  21. fmt.Println(&quot;Length of the binary is not in the length of 8&quot;)
  22. }
  23. return result
  24. }

huangapple
  • 本文由 发表于 2014年3月19日 05:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/22491876.html
匿名

发表评论

匿名网友

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

确定