doushaiyu5065
2016-01-10 02:00
浏览 210
已采纳

将[] byte转换为Little / Big-Endian Signed Integer或Float?

I am able to convert []byte into unsigned integers:

a := binary.LittleEndian.Uint16(sampleA)
b := binary.BigEndian.Uint32(sampleB)

This leverages the BigEndian and LittleEndian types within the Go package https://golang.org/src/encoding/binary/binary.go

This provides Uint16() however there are no equivalent Int16() or Float32()

Any thoughts on why not? Also, how ought this be done?

图片转代码服务由CSDN问答提供 功能建议

我能够将 [] byte 转换为无符号整数: \ n

  a:= binary.LittleEndian.Uint16(sampleA)
b:= binary.BigEndian.Uint32(sampleB)
   
 
 

此 利用Go包中的BigEndian和LittleEndian类型 https://golang.org/src/encoding /binary/binary.go

这提供了 Uint16(),但是没有等效的 Int16()或< code> Float32()

对为什么没有的任何想法? 另外,应该怎么做呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxin20081125 2016-01-10 02:33
    已采纳

    Converting numeric types into a series of bytes ([]byte) and vice versa is about the endianness. How you interpret the result is entirely up to you.

    All you need is to assemble a 16-bit, 32-bit or 64-bit value, once it's done, you can interpret the result as you want.

    For example if you already have a uint16 value, to use it as a signed value, all you need is a type conversion because the memory layout of an uint16 and int16 is the same (converting from uint16 to int16 doesn't change the memory representation just the type):

    a := binary.LittleEndian.Uint16(sampleA)
    // If you need int16:
    a2 := int16(a)
    

    Similarly:

    a := binary.LittleEndian.Uint64(sampleA)
    // If you need int64:
    a2 := int64(a)
    

    The situation is a little more complicated with uint -> float conversion as using a simple type conversion would try to convert the numeric value and not just change the type (and thus would change the memory representation).

    For converting unsigned integers to float types, you can use functions of the math package, namely math.Float32frombits() and math.Float64frombits(), and for the reverse direction (converting a float value to an unsigned integer) having the same memory layout: math.Float32bits() and math.Float64bits().

    For example:

    a := binary.LittleEndian.Uint64(sampleA)
    // If you need a float64:
    a2 := math.Float64frombits(a)
    

    If you would look into the implementation of these functions from the math package, you can see that the memory value/layout is not manipulated, it is just "viewed" as a different type, by using the unsafe package. For example:

    func Float32frombits(b uint32) float32 { return *(*float32)(unsafe.Pointer(&b)) }
    

    As mentioned by Paul, the binary package provides Read() and Write() functions to do these conversions under the hood so you don't need to.

    Showcasing using the same "pi" example (from the doc of binary.Read()):

    b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
    
    // USING binary.Read()
    var pi float64
    buf := bytes.NewReader(b)
    err := binary.Read(buf, binary.LittleEndian, &pi)
    if err != nil {
        fmt.Println("binary.Read failed:", err)
    }
    fmt.Println(pi)
    
    // Using LittleEndian.Uint64() and math.Float64frombits()
    a := binary.LittleEndian.Uint64(b)
    a2 := math.Float64frombits(a)
    fmt.Println(a2)
    

    Output (try it on the Go Playground):

    3.141592653589793
    3.141592653589793
    
    已采纳该答案
    打赏 评论
  • duanpai9945 2016-01-10 02:07

    The ByteOrder type provides a low-level API for decoding binary values. To read float64 or other types, you can use binary.Read. There's an example on the godoc page for the binary package, which I've copied here:

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

    There's no functions for decoding float16, because that's not a type in Go.

    打赏 评论

相关推荐 更多相似问题