gpt4 book ai didi

python - 去如何实现python binascii.unhexlify方法?

转载 作者:数据小太阳 更新时间:2023-10-29 03:42:40 24 4
gpt4 key购买 nike

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

问题

Python binascii.unhexlify 看起来很复杂,我不知道在 go 中实现它很热。

最佳答案

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!
}

关于python - 去如何实现python binascii.unhexlify方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com