package hex

import "encoding/hex"

Package hex 实现十六进制编码和解码。

Index

Examples

Variables

var ErrLength = errors.New("encoding/hex: odd length hex string")

ErrLength 报告尝试使用 Decode 或 DecodeString 解码奇数长度的输入。 基于流的 Decoder 返回 io.ErrUnexpectedEOF 而不是 ErrLength。

Functions

func AppendDecode

func AppendDecode(dst, src []byte) ([]byte, error)

AppendDecode 将十六进制解码后的 src 追加到 dst 并返回扩展后的缓冲区。 如果输入格式错误,它返回部分解码的 src 和一个错误。

func AppendEncode

func AppendEncode(dst, src []byte) []byte

AppendEncode 将十六进制编码后的 src 追加到 dst 并返回扩展后的缓冲区。

func Decode

func Decode(dst, src []byte) (int, error)

Decode 将 src 解码到 DecodedLen(len(src)) 个字节中, 返回实际写入 dst 的字节数。

Decode 要求 src 只包含十六进制字符,且 src 长度为偶数。 如果输入格式错误,Decode 返回出错之前 已解码的字节数。

Example
package main

import (
	"encoding/hex"
	"fmt"
	"log"
)

func main() {
	src := []byte("48656c6c6f20476f7068657221")

	dst := make([]byte, hex.DecodedLen(len(src)))
	n, err := hex.Decode(dst, src)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", dst[:n])

}

Output:

Hello Gopher!

func DecodeString

func DecodeString(s string) ([]byte, error)

DecodeString 返回十六进制字符串 s 所表示的字节。

DecodeString 要求 src 只包含十六进制字符,且 src 长度为偶数。 如果输入格式错误,DecodeString 返回出错之前 已解码的字节。

Example
package main

import (
	"encoding/hex"
	"fmt"
	"log"
)

func main() {
	const s = "48656c6c6f20476f7068657221"
	decoded, err := hex.DecodeString(s)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", decoded)

}

Output:

Hello Gopher!

func DecodedLen

func DecodedLen(x int) int

DecodedLen 返回 x 个源字节的解码长度。 具体来说,它返回 x / 2。

func Dump

func Dump(data []byte) string

Dump 返回一个包含给定数据的十六进制转储的字符串。该十六进制转储的格式 与命令行中 `hexdump -C` 的输出一致。

Example
package main

import (
	"encoding/hex"
	"fmt"
)

func main() {
	content := []byte("Go is an open source programming language.")

	fmt.Printf("%s", hex.Dump(content))

}

Output:

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.|

func Dumper

func Dumper(w io.Writer) io.WriteCloser

Dumper 返回一个 io.WriteCloser,它将所有写入数据的十六进制转储写入 w。转储的格式与命令行中 `hexdump -C` 的输出 一致。

Example
package main

import (
	"encoding/hex"
	"os"
)

func main() {
	lines := []string{
		"Go is an open source programming language.",
		"\n",
		"We encourage all Go users to subscribe to golang-announce.",
	}

	stdoutDumper := hex.Dumper(os.Stdout)

	defer stdoutDumper.Close()

	for _, line := range lines {
		stdoutDumper.Write([]byte(line))
	}

}

Output:

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.|

func Encode

func Encode(dst, src []byte) int

Encode 将 src 编码到 EncodedLen(len(src)) 个字节的 dst 中。 为方便起见,它返回写入 dst 的字节数, 但该值始终等于 EncodedLen(len(src))。 Encode 实现十六进制编码。

Example
package main

import (
	"encoding/hex"
	"fmt"
)

func main() {
	src := []byte("Hello Gopher!")

	dst := make([]byte, hex.EncodedLen(len(src)))
	hex.Encode(dst, src)

	fmt.Printf("%s\n", dst)

}

Output:

48656c6c6f20476f7068657221

func EncodeToString

func EncodeToString(src []byte) string

EncodeToString 返回 src 的十六进制编码。

Example
package main

import (
	"encoding/hex"
	"fmt"
)

func main() {
	src := []byte("Hello")
	encodedStr := hex.EncodeToString(src)

	fmt.Printf("%s\n", encodedStr)

}

Output:

48656c6c6f

func EncodedLen

func EncodedLen(n int) int

EncodedLen 返回 n 个源字节的编码长度。 具体来说,它返回 n * 2。

func NewDecoder

func NewDecoder(r io.Reader) io.Reader

NewDecoder 返回一个 io.Reader,它从 r 解码十六进制字符。 NewDecoder 要求 r 只包含偶数个十六进制字符。

func NewEncoder

func NewEncoder(w io.Writer) io.Writer

NewEncoder 返回一个 io.Writer,它将小写十六进制字符写入 w。

Types

type InvalidByteError

type InvalidByteError byte

InvalidByteError 值描述由十六进制字符串中的无效字节导致的错误。

func (InvalidByteError) Error
func (e InvalidByteError) Error() string