user1529891
user1529891

Reputation:

How to convert []byte data to uint16 in go?

I have the following hex data: 0xB01B; its 45083 as uint16; how do I convert this to uint16 in go?

Upvotes: 5

Views: 5457

Answers (2)

Zombo
Zombo

Reputation: 1

Another option is ReadUint16:

package main
import "golang.org/x/crypto/cryptobyte"

func main() {
   s := cryptobyte.String{0xB0, 0x1B}
   var n uint16
   s.ReadUint16(&n)
   println(n == 45083)
}

https://godocs.io/golang.org/x/crypto/cryptobyte#String.ReadUint16

Upvotes: 0

user142162
user142162

Reputation:

Use the encoding/binary package:

import (
   "encoding/binary"
)

data := []byte{0xB0, 0x1B}
val := binary.BigEndian.Uint16(data)

https://play.golang.org/p/wHW8KDgls9

Upvotes: 13

Related Questions