Pole_Zhang
Pole_Zhang

Reputation: 1457

Decoding data from a byte slice to Uint32

package main

import (
        "bytes"
        "encoding/binary"
        "fmt"
)

func main() {
        aa := uint(0xFFFFFFFF)
        fmt.Println(aa)
        byteNewbuf := []byte{0xFF, 0xFF, 0xFF, 0xFF}
        buf := bytes.NewBuffer(byteNewbuf)
        tt, _ := binary.ReadUvarint(buf)
        fmt.Println(tt)
}

Need to convert 4 bytes array to uint32 but why the results are not same ? go verion : beta 1.1

Upvotes: 9

Views: 16920

Answers (3)

James Henstridge
James Henstridge

Reputation: 43909

You can do this with one of the ByteOrder objects from the encoding/binary package. For instance:

package main

import (
        "encoding/binary"
        "fmt"
)

func main() {
        aa := uint(0x7FFFFFFF)
        fmt.Println(aa)
        slice := []byte{0xFF, 0xFF, 0xFF, 0x7F}
        tt := binary.LittleEndian.Uint32(slice)
        fmt.Println(tt)
}

If your data is in big endian format, you can instead use the same methods on binary.BigEndian.

Upvotes: 13

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54089

Here is how to use the encoding/binary package to do what you want. Note that you don't want to use any of the var functions as those do variable length encoding.

Playground version

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "log"
)

func main() {
    aa := uint(0xFFFFFF0F)
    fmt.Println(aa)
    tt := uint32(0)

    byteNewbuf := []byte{0x0F, 0xFF, 0xFF, 0xFF}
    buf := bytes.NewBuffer(byteNewbuf)
    err := binary.Read(buf, binary.LittleEndian, &tt)
    if err != nil {
        log.Fatalf("Decode failed: %s", err)
    }
    fmt.Println(tt)
}

Result is

4294967055
4294967055

Upvotes: 4

zzzz
zzzz

Reputation: 91253

tt := uint32(buf[0])<<24 | uint32(buf[1])<<16 | uint32(buf[2]) <<8 |
        uint32(buf[3])

for BE or

tt := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2]) <<16 |
        uint32(buf[3]) <<24

for LE.

[u]varint is a different kind of encoding (32 bit numbers can have as much as 5 bytes in the encoded form, 64 bit numbers up to 10).

  • No need to create a buffer for []byte. Use Varint or Uvarint directly on the byte slice instead.

  • You're throwing away the error returned by the function. The second result indicates how many bytes were read or if there was a problem. There is a problem while decoding 0xff, 0xff, 0xff, 0xff as an uvarint.

Upvotes: 5

Related Questions