?
This document uses PHP Chinese website manual Release
import "encoding/hex"
Overview
Index
Examples
包十六進制實現(xiàn)十六進制編碼和解碼。
Variables
func Decode(dst, src []byte) (int, error)
func DecodeString(s string) ([]byte, error)
func DecodedLen(x int) int
func Dump(data []byte) string
func Dumper(w io.Writer) io.WriteCloser
func Encode(dst, src []byte) int
func EncodeToString(src []byte) string
func EncodedLen(n int) int
type InvalidByteError
func (e InvalidByteError) Error() string
解碼DecodeString轉(zhuǎn)儲轉(zhuǎn)儲器編碼EncodeToString
hex.go
ErrLength是解碼奇數(shù)長度片的結(jié)果。
var ErrLength = errors.New("encoding/hex: odd length hex string")
func Decode(dst, src []byte) (int, error)
Decode將src解碼為DecodedLen(len(src))字節(jié),返回寫入dst的實際字節(jié)數(shù)。
解碼期望src只包含十六進制字符,并且src應(yīng)該有一個均勻的長度。
package mainimport ("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])}
func DecodeString(s string) ([]byte, error)
DecodeString返回由十六進制字符串s表示的字節(jié)。
package mainimport ("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)}
func DecodedLen(x int) int
DecodedLen返回x個源字節(jié)解碼的長度。具體來說,它返回x / 2。
func Dump(data []byte) string
轉(zhuǎn)儲返回一個包含給定數(shù)據(jù)的十六進制轉(zhuǎn)儲的字符串。十六進制轉(zhuǎn)儲的格式與hexdump -C
命令行上的輸出相匹配。
package mainimport ("encoding/hex""fmt")func main() { content := []byte("Go is an open source programming language.") fmt.Printf("%s", hex.Dump(content))}
func Dumper(w io.Writer) io.WriteCloser
Dumper返回一個WriteCloser,它將所有寫入數(shù)據(jù)的十六進制轉(zhuǎn)儲寫入w。轉(zhuǎn)儲的格式與hexdump -C
命令行上的輸出相匹配。
package mainimport ("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))}}
func Encode(dst, src []byte) int
將編碼src編碼為dst的EncodedLen(len(src))字節(jié)。為了方便起見,它返回寫入dst的字節(jié)數(shù),但該值始終為EncodedLen(len(src))。編碼實現(xiàn)十六進制編碼。
package mainimport ("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)}
func EncodeToString(src []byte) string
EncodeToString返回src的十六進制編碼。
package mainimport ("encoding/hex""fmt")func main() { src := []byte("Hello") encodedStr := hex.EncodeToString(src) fmt.Printf("%s\n", encodedStr)}
func EncodedLen(n int) int
EncodedLen返回n個源字節(jié)的編碼長度。具體來說,它返回n * 2。
InvalidByteError值描述由十六進制字符串中的無效字節(jié)導(dǎo)致的錯誤。
type InvalidByteError byte
func (e InvalidByteError) Error() string