-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_example_test.go
100 lines (74 loc) · 2.13 KB
/
bit_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package bit
import (
"bytes"
"fmt"
"io"
"log"
"os"
)
func ExampleEncode() {
src := []byte("Hello Gopher!")
dst := make([]byte, EncodedLen(len(src)))
Encode(dst, src)
fmt.Printf("%s\n", dst)
// Output:
// 01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001
}
func ExampleEncodeToString() {
src := []byte("Hello Gopher!")
encodedStr := EncodeToString(src)
fmt.Printf("%s\n", encodedStr)
// Output:
// 01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001
}
func ExampleNewEncoder() {
src := []byte("Hello Gopher!")
enc := NewEncoder(os.Stdout)
enc.Write(src)
// Output:
// 01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001
}
func ExampleDecode() {
src := []byte("01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001")
dst := make([]byte, DecodedLen(len(src)))
n, err := Decode(dst, src)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", dst[:n])
// Output:
// Hello Gopher!
}
func ExampleDecodeString() {
const s = "01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001"
decoded, err := DecodeString(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", decoded)
// Output:
// Hello Gopher!
}
func ExampleNewDecoder() {
src := []byte("01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001")
buf := bytes.NewBuffer(src)
dec := NewDecoder(buf)
io.Copy(os.Stdout, dec)
// Output:
// Hello Gopher!
}
func ExampleDump() {
dump := Dump([]byte("dump test"))
fmt.Printf("%s\n", dump)
// Output:
// 00000000: 01100100 01110101 01101101 01110000 00100000 01110100 dump t
// 00000006: 01100101 01110011 01110100 est
}
func ExampleDumper() {
d := Dumper(os.Stdout)
d.Write([]byte("dump test"))
d.Close()
// Output:
// 00000000: 01100100 01110101 01101101 01110000 00100000 01110100 dump t
// 00000006: 01100101 01110011 01110100 est
}