Kevin Burke
Kevin Burke

Reputation: 64864

Force go to read multiple bytes for a Uvarint

Lets say I have the following 2 byte array, that I've read from a file.

bits := []byte{3, 223}

I would like to interpret this as one integer, which would be 991 (0b11 from the first number, 0b11011111 from the second). I'm trying to do this with Go and running into difficulty.

import "encoding/binary"
import "fmt"

bits := []byte{3, 223}
fmt.Println(binary.Uvarint(bits))

This reads the "3" and then stops. Similarly for binary.Read... etc.

I'm sure there is some idiom that I am missing here, and would appreciate your help.

Thanks, Kevin

Upvotes: 2

Views: 181

Answers (1)

Kevin Burke
Kevin Burke

Reputation: 64864

Ah, I needed to use the ByteOrder constructor

import "encoding/binary"
import "fmt"

bits := []byte{3, 223}
fmt.Println(binary.BigEndian.Uint16(bits))

Upvotes: 4

Related Questions