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 
posted @ 2020-04-01 20:50  Kingram  阅读(11875)  评论(0编辑  收藏  举报