パッケージ hex
import "encoding/hex"
- 概要
- 目次
- 例
hex パッケージは 16 進法のエンコード,デコードを実装します。
変数
ErrLength は, Decode または DecodeString を使用して奇数長の入力をデコードしようとしたことを報告します。
ストリームベースのデコーダは, ErrLength の代わりに io.ErrUnexpectedEOF を返します。
var ErrLength = errors.New("encoding/hex: odd length hex string")
func Decode(dst, src []byte) (int, error)
Decode (デコード) は src を DecodedLen(len(src)) バイトにデコードし, dst に書き込まれた実際のバイト数を返します。
デコードは, src が 16 進文字のみを含み, src が偶数の長さを持つことを想定しています。
入力が不正な形式の場合, Decode はエラーの前にデコードされたバイト数を返します。
▾ 例
コード:
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])
出力:
Hello Gopher!
func DecodeString(s string) ([]byte, error)
DecodeString は, 16 進数文字列 s で表されるバイトを返します。
DecodeString は, src が 16 進文字のみを含み, src が偶数の長さを持つことを期待しています。
入力が不正な形式の場合, DecodeString はエラーの前にデコードされたバイトを返します。
▾ 例
コード:
const s = "48656c6c6f20476f7068657221"
decoded, err := hex.DecodeString(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", decoded)
出力:
Hello Gopher!
func DecodedLen(x int) int
DecodedLen は, x 個のソースバイトのデコードの長さを返します。
具体的には, x / 2 を返します。
func Dump(data []byte) string
Dump は与えられたデータの 16 進ダンプを含む文字列を返します。
16 進ダンプのフォーマットは,コマンド行の `hexdump -C` の出力と一致します。
▾ 例
コード:
content := []byte("Go is an open source programming language.")
fmt.Printf("%s", hex.Dump(content))
出力:
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(w io.Writer) io.WriteCloser
Dumper は,書き込まれたすべてのデータの 16 進ダンプを w に書き込む WriteCloser を返します。
ダンプのフォーマットはコマンドラインの `hexdump -C` の出力と一致します。
▾ 例
コード:
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))
}
出力:
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(dst, src []byte) int
Encode は src を dst の EncodedLen(len(src)) バイトにエンコードします。
便宜上, dst に書き込まれたバイト数を返しますが,この値は常に EncodedLen(len(src)) です。
Encode は 16 進エンコードを実装します。
▾ 例
コード:
src := []byte("Hello Gopher!")
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
fmt.Printf("%s\n", dst)
出力:
48656c6c6f20476f7068657221
func EncodeToString(src []byte) string
EncodeToString は, src の 16 進エンコーディングを返します。
▾ 例
コード:
src := []byte("Hello")
encodedStr := hex.EncodeToString(src)
fmt.Printf("%s\n", encodedStr)
出力:
48656c6c6f
func EncodedLen(n int) int
EncodedLen は, n ソースバイトのエンコードの長さを返します。
具体的には, n * 2 を返します。
func NewDecoder(r io.Reader) io.Reader
NewDecoder は, r から 16 進文字をデコードする io.Reader を返します。
NewDecoder は, r に含まれるのは偶数個の 16 進文字のみであることを想定しています。
func NewEncoder(w io.Writer) io.Writer
NewEncoder は,小文字の 16 進文字を w に書き込む io.Writer を返します。
InvalidByteError 値は, 16 進文字列内の無効なバイトがあったことを示すエラーです。
type InvalidByteError byte
func (InvalidByteError) Error
¶
func (e InvalidByteError) Error() string