如何实现python binascii.unhexlify方法?

问题描述 投票:-2回答:1

在我的公司有一个用python编写的系统,我想用golang重新实现它。

Question

Python binascii.unhexlify似乎很复杂,我不知道在go中实现它。

python python-3.x go
1个回答
-1
投票

binascii.unhexlify方法很简单。它只是从十六进制转换为二进制。每两个十六进制数字是一个8位字节(256个可能的值)。这是我的代码

func unhexlify(str string) []byte {
    res := make([]byte, 0)
    for i := 0; i < len(str); i+=2 {
        x, _ := strconv.ParseInt(str[i:i+2], 16, 32)
        res = append(res, byte(x))
    }
    return res
}

我应该使用这个库

func ExampleDecodeString() {
    const s = "48656c6c6f20476f7068657221"
    decoded, err := hex.DecodeString(s)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%s\n", decoded)

    // Output:
    // Hello Gopher!
}
© www.soinside.com 2019 - 2024. All rights reserved.