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 
",b)
	fmt.Printf("@@@@--string-->%s 
",encodedStr)
}

运行结果

@@@@--string-->ff68b4ff 
@@@@--bytes-->ff68b4ff 
原文地址:https://www.cnblogs.com/Kingram/p/12615667.html