dongyulian5801
2017-10-30 13:09
浏览 76
已采纳

尝试在Golang中将字节片解析为基数16

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

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

我正在尝试将具有以下值 020000 的字节片解析为 以16为基数的数字,但尚未生效。 我在做什么错了?

 包main 
 
import(
“ fmt” 
“ strconv” 
)
 
func main(){
 输入:= [] byte {0,2,0,0,0,0} 
期望:= 131072 
 
实际:= headerVersion(input)
 
如果实际!=期望{
恐慌(  fmt.Sprintf(“期望的%v但得到了%v。”,期望,实际))
} 
} 
 
func headerVersion(input [] byte)int {
输出,_:= strconv.ParseUint( 字符串(输入),16、64)
返回int(输出)
} 
   
 
 

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

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

2条回答 默认 最新

  • dongshang4984 2017-10-30 13:33
    已采纳

    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.

    已采纳该答案
    打赏 评论
  • doutang1856 2017-10-30 13:31

    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'}
    
    打赏 评论

相关推荐 更多相似问题