简体   繁体   中英

Trying to parse byte slice as base 16 number in Golang

I'm trying to parse a slice of bytes with the below value 020000 to as a base 16 number but haven't gotten it working yet. What am I doing wrong?

package main

import (
    "fmt"
    "strconv"
)

func main() {
    input := []byte{0, 2, 0, 0, 0, 0}
    expectation := 131072

    actual := headerVersion(input)

    if actual != expectation {
        panic(fmt.Sprintf("Expected %v but got %v.", expectation, actual))
    }
}

func headerVersion(input []byte) int {
    output, _ := strconv.ParseUint(string(input), 16, 64)
    return int(output)
}

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

Never omit errors, always check them!

func headerVersion(input []byte) int {
    output, err := strconv.ParseUint(string(input), 16, 64)
    if err != nil {
        fmt.Println(err)
    }
    return int(output)
}

Feeding []byte{0, 2, 0, 0, 0, 0} to it, it will print:

strconv.ParseUint: parsing "\x00\x02\x00\x00\x00\x00": invalid syntax

When you do string(input) , it converts the bytes of input interpreted as the UTF-8 encoded bytes of a text.

But input contains the numerical values of the digits, not the UTF-8 representation! The numerical value for a 0 digit is '0' , which equals to 48 .

So you have to shift the byte values by '0' (this demonstration only works if byte values are less than 10!):

func headerVersion(input []byte) int {
    for i := range input {
        input[i] += '0'
    }
    output, err := strconv.ParseUint(string(input), 16, 64)
    if err != nil {
        fmt.Println(err)
    }

    return int(output)
}

Your original headerVersion() function would only give correct, valid result if you would feed it the following input:

input := []byte{'0', '2', '0', '0', '0', '0'}

You've got a raw byte slice that is the bytes making up the number you want, but you're instead parsing it as if it were the bytes making up the string representation of the bytes making up the number you want. Rather than attempting to parse as a string, parse as what it is - bytes. You can do this using the binary package , which, per its documentation:

implements simple translation between numbers and byte sequences and encoding and decoding of varints.

Which is exactly what you want. How you use it will depend on the endianness and encoding of the data, but the documentation should get you going in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM