Golang十六進制字符串和byte數組互轉
需求
Golang十六進制字符串和byte數組互相轉換,使用"encoding/hex"包
實現Demo
package main
import (
"encoding/hex"
"fmt"
)
func main() {
str := "ff68b4ff"
b, _ := hex.DecodeString(str)
encodedStr := hex.EncodeToString(b)
fmt.Printf("@@@@--bytes-->%02x \n",b)
fmt.Printf("@@@@--string-->%s \n",encodedStr)
}
運行結果
@@@@--string-->ff68b4ff
@@@@--bytes-->ff68b4ff