无法将我的字符串转换为十六进制消息

Raj*_*h S 1 go

下面有x,这是我期望的字符串,我试图重新创建y自己以匹配我的期望的字符串。基本上是尝试转换"01""\x01"打印时得到相同的字节。

现在,当我打印时[]byte(x)[]byte(y)我希望它们是相同的,但不是。请帮助我重新创建,x"01"作为输入。

package main

import (
    "fmt"
)

func main() {
    //Expected string
    x := "\x01"
    //Trying to convert my 01 in string form to same as above - Basically recreate above string again
    y := "\\x" + "01"
    fmt.Println(x)
    fmt.Println(y)
    fmt.Println([]byte(x))
    fmt.Println([]byte(y))
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*h S 5

这就是我想要的-解决了我的问题!:)谢谢大家

import (
    "fmt"
    "encoding/hex"
)

func main() {
    //Expected string
    x := "\x01"
    //Trying to convert my 01 in string form to same as above - Basically recreate above string again
    y,err :=  hex.DecodeString("01")
    if err != nil {
            panic(err)
    }
    fmt.Println(x)
    fmt.Println(y)
    fmt.Println([]byte(x))
    fmt.Println([]byte(y))
}
Run Code Online (Sandbox Code Playgroud)