| | |
| | |
| | |
| |
|
| | package hex_test |
| |
|
| | import ( |
| | "encoding/hex" |
| | "fmt" |
| | "log" |
| | "os" |
| | ) |
| |
|
| | func ExampleEncode() { |
| | src := []byte("Hello Gopher!") |
| |
|
| | dst := make([]byte, hex.EncodedLen(len(src))) |
| | hex.Encode(dst, src) |
| |
|
| | fmt.Printf("%s\n", dst) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleDecode() { |
| | 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 ExampleDecodeString() { |
| | const s = "48656c6c6f20476f7068657221" |
| | decoded, err := hex.DecodeString(s) |
| | if err != nil { |
| | log.Fatal(err) |
| | } |
| |
|
| | fmt.Printf("%s\n", decoded) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleDump() { |
| | content := []byte("Go is an open source programming language.") |
| |
|
| | fmt.Printf("%s", hex.Dump(content)) |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleDumper() { |
| | 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 ExampleEncodeToString() { |
| | src := []byte("Hello") |
| | encodedStr := hex.EncodeToString(src) |
| |
|
| | fmt.Printf("%s\n", encodedStr) |
| |
|
| | |
| | |
| | } |
| |
|