encoding/hex 官方文档
https://studygolang.com/static/pkgdoc/pkg/encoding_hex.htm#Encode
func EncodedLen(n int) int
长度x的明文数据编码后的编码数据的长度。
func Encode(dst, src []byte) int
将src的数据解码为EncodedLen(len(src))字节,返回实际写入dst的字节数:EncodedLen(len(src))。
func EncodeToString(src []byte) string
将数据src编码为字符串s。
func DecodedLen(x int) int
长度x的编码数据 解码后的明文数据的长度
func Decode(dst, src []byte) (int, error)
将 src 解码为 DecodedLen(len(src)) 字节,返回实际写入dst 的字节数;如遇到非法字符,返回描述错误的error。
func DecodeString(s string) ([]byte, error)
返回hex编码的字符串s代表的数据
func Dump(data []byte) string
返回给定数据的hex dump格式的字符串,这个字符串与控制台下`hexdump -C`对该数据的输出是一致的。
func Dumper(w io.Writer) io.WriteCloser
返回一个io.WriteCloser接口,将写入的数据的hex dump格式写入w,具体格式为'hexdump -C'。
package main
import (
"encoding/hex"
"fmt"
"log"
"os"
)
func main() {
srccode := []byte("Hello world!")
fmt.Printf("srccode(%T) = %v\n", srccode, srccode)
// 编码部分
fmt.Println("-------------------------------编码部分---------------------------------")
srccodeLen := len(srccode)
fmt.Printf("srccodeLen(%T) = %v\n", srccodeLen, srccodeLen)
// hex.EncodedLen(len(srcCode)) 返回值实际是 len(srcCode) 长度的2倍
dstEncode := make([]byte, hex.EncodedLen(len(srccode)))
// 将 srcCode 编码为十六进制,返回编码后的长度
encodeLen := hex.Encode(dstEncode, srccode)
fmt.Printf("encodeLen(%T) = %v\n", encodeLen, encodeLen)
fmt.Printf("dstEncode(%T) = %v\n", dstEncode, dstEncode)
// 将 srcCode 编码为字符串
encodedStr := hex.EncodeToString(srccode)
fmt.Printf("encodedStr(%T) = %v\n", encodedStr, encodedStr)
// 解码部分
fmt.Println("-------------------------------解码部分---------------------------------")
dstEncodeLen := len(dstEncode)
fmt.Printf("dstEncodeLen(%T) = %v\n", dstEncodeLen, dstEncodeLen)
// hex.DecodedLen(len(dstEncode)) 返回值实际是len(dstEncode) 长度一般
dstDeCode := make([]byte, hex.DecodedLen(len(dstEncode)))
// 将十六进制解码为字符串,返回解码后的长度
decodeLen, err := hex.Decode(dstDeCode, dstEncode)
if err != nil {
log.Fatal(err)
}
fmt.Printf("decodeLen(%T) = %v\n", decodeLen, decodeLen)
fmt.Printf("dstDeCode(%T) = %v\n", dstDeCode, dstDeCode)
// 返回十六进制字符串表示的字节
decodedStr, err := hex.DecodeString(string(dstEncode))
fmt.Printf("decodedStr(%T) = %v\n", decodedStr, decodedStr)
fmt.Println("-------------------------------dump部分---------------------------------")
content := []byte("Go is an open source programming language.")
// 转储返回一个包含给定数据的十六进制转储的字符串。十六进制转储的格式与hexdump -C命令行上的输出相匹配
fmt.Printf("dump(%T) = \n%v\n", hex.Dump(content), hex.Dump(content))
fmt.Println("-------------------------------dumper部分---------------------------------")
lines := []string{
"Go is an open source programming language.",
"\n",
"We encourage all Go users to subscribe to golang-announce.",
}
//Dumper 返回一个 WriteCloser,它将所有写入数据的十六进制转储写入 w。转储的格式与hexdump -C命令行上的输出相匹配
stdoutDumper := hex.Dumper(os.Stdout)
defer stdoutDumper.Close()
for _, line := range lines {
stdoutDumper.Write([]byte(line))
}
}
运行结果:
srccode([]uint8) = [72 101 108 108 111 32 119 111 114 108 100 33]
-------------------------------编码部分---------------------------------
srccodeLen(int) = 12
encodeLen(int) = 24
dstEncode([]uint8) = [52 56 54 53 54 99 54 99 54 102 50 48 55 55 54 102 55 50 54 99 54 52 50 49]
encodedStr(string) = 48656c6c6f20776f726c6421
-------------------------------解码部分---------------------------------
dstEncodeLen(int) = 24
decodeLen(int) = 12
dstDeCode([]uint8) = [72 101 108 108 111 32 119 111 114 108 100 33]
decodedStr([]uint8) = [72 101 108 108 111 32 119 111 114 108 100 33]
-------------------------------dump部分---------------------------------
dump(string) =
00000000 47 6f 20 69 73 20 61 6e 20 6f 70 65 6e 20 73 6f |Go is an open so|
00000010 75 72 63 65 20 70 72 6f 67 72 61 6d 6d 69 6e 67 |urce programming|
00000020 20 6c 61 6e 67 75 61 67 65 2e | language.|
-------------------------------dumper部分---------------------------------
00000000 47 6f 20 69 73 20 61 6e 20 6f 70 65 6e 20 73 6f |Go is an open so|
00000010 75 72 63 65 20 70 72 6f 67 72 61 6d 6d 69 6e 67 |urce programming|
00000020 20 6c 61 6e 67 75 61 67 65 2e 0a 57 65 20 65 6e | language..We en|
00000030 63 6f 75 72 61 67 65 20 61 6c 6c 20 47 6f 20 75 |courage all Go u|
00000040 73 65 72 73 20 74 6f 20 73 75 62 73 63 72 69 62 |sers to subscrib|
00000050 65 20 74 6f 20 67 6f 6c 61 6e 67 2d 61 6e 6e 6f |e to golang-anno|
00000060 75 6e 63 65 2e |unce.|