Documentation
¶
Overview ¶
hexパッケージは16進数のエンコードとデコードを実装します。
Index ¶
- Variables
- func AppendDecode(dst, src []byte) ([]byte, error)
- func AppendEncode(dst, src []byte) []byte
- 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
- func NewDecoder(r io.Reader) io.Reader
- func NewEncoder(w io.Writer) io.Writer
- type InvalidByteError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrLength = errors.New("encoding/hex: odd length hex string")
ErrLengthは、Decode または DecodeString を使用して奇数長の入力をデコードしようとする試みを報告します。 ストリームベースのDecoderは、ErrLengthの代わりに io.ErrUnexpectedEOF を返します。
Functions ¶
func AppendDecode ¶ added in v1.22.0
AppendDecodeは、16進数でデコードされたsrcをdstに追加し、 拡張されたバッファを返します。 入力が不正な形式の場合、部分的にデコードされたsrcとエラーを返します。
func AppendEncode ¶ added in v1.22.0
AppendEncodeは、16進数でエンコードされたsrcをdstに追加し、 拡張されたバッファを返します。
func Decode ¶
Decodeは、srcを DecodedLen(len(src))バイトにデコードし、 dstに書き込まれた実際のバイト数を返します。
Decodeは、srcが16進文字のみを含み、かつsrcの長さが偶数であることを期待しています。 もし入力が不正な場合、Decodeはエラーが発生する前にデコードされたバイト数を返します。
Example ¶
package main
import (
"github.com/shogo82148/std/encoding/hex"
"github.com/shogo82148/std/fmt"
"github.com/shogo82148/std/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 ¶
DecodeStringは16進数の文字列sによって表されるバイトを返します。
DecodeStringは、srcが16進数の文字のみを含み、かつ偶数の長さであることを期待しています。 入力が不正な場合、DecodeStringはエラーが発生する前にデコードされたバイトを返します。
Example ¶
package main
import (
"github.com/shogo82148/std/encoding/hex"
"github.com/shogo82148/std/fmt"
"github.com/shogo82148/std/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 Dump ¶
Dumpは指定されたデータの16進ダンプを含む文字列を返します。16進ダンプの形式は、コマンドラインの`hexdump -C`の出力と一致します。
Example ¶
package main
import (
"github.com/shogo82148/std/encoding/hex"
"github.com/shogo82148/std/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は、書き込まれたすべてのデータの16進ダンプをwに書き込む io.WriteCloser を返します。 ダンプの形式は、コマンドライン上の`hexdump -C`の出力と一致します。
Example ¶
package main
import (
"github.com/shogo82148/std/encoding/hex"
"github.com/shogo82148/std/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 ¶
Encodeは、srcを EncodedLen(len(src))バイトのdstにエンコードします。 便宜上、dstに書き込まれたバイト数を返しますが、この値は常に EncodedLen(len(src))です。 Encodeは16進数エンコーディングを実装します。
Example ¶
package main
import (
"github.com/shogo82148/std/encoding/hex"
"github.com/shogo82148/std/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 ¶
EncodeToStringは、srcの16進数エンコーディングを返します。
Example ¶
package main
import (
"github.com/shogo82148/std/encoding/hex"
"github.com/shogo82148/std/fmt"
)
func main() {
src := []byte("Hello")
encodedStr := hex.EncodeToString(src)
fmt.Printf("%s\n", encodedStr)
}
Output: 48656c6c6f
func EncodedLen ¶
EncodedLenはn個の元のバイトのエンコーディングの長さを返します。 具体的には、n * 2を返します。
func NewDecoder ¶ added in v1.10.0
NewDecoderは、rから16進数文字をデコードする io.Reader を返します。 NewDecoderは、rが偶数個の16進数文字のみを含むことを期待します。
Types ¶
type InvalidByteError ¶
type InvalidByteError byte
InvalidByteError の値は、16 進数文字列に無効なバイトが含まれている場合のエラーを記述します。
func (InvalidByteError) Error ¶
func (e InvalidByteError) Error() string