Golang十六进制字符串和byte数组互转

时间:2023-03-09 20:29:54
Golang十六进制字符串和byte数组互转

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