In my company there is a system written in python and I want to reimplement it using golang.
Python binascii.unhexlify
seems complex, and I don't know hot to implement it in go.
binascii.unhexlify
method is simple enough. It's just converting from hex to binary. Every two hex digits is an 8-bit byte (256 possible values). here is my code
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
}
I should use the library
func ExampleDecodeString() {
const s = "48656c6c6f20476f7068657221"
decoded, err := hex.DecodeString(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s
", decoded)
// Output:
// Hello Gopher!
}