1
2
3
4
5 package binary_test
6
7 import (
8 "bytes"
9 "encoding/binary"
10 "fmt"
11 "math"
12 )
13
14 func ExampleWrite() {
15 buf := new(bytes.Buffer)
16 var pi float64 = math.Pi
17 err := binary.Write(buf, binary.LittleEndian, pi)
18 if err != nil {
19 fmt.Println("binary.Write failed:", err)
20 }
21 fmt.Printf("% x", buf.Bytes())
22
23 }
24
25 func ExampleWrite_multi() {
26 buf := new(bytes.Buffer)
27 var data = []interface{}{
28 uint16(61374),
29 int8(-54),
30 uint8(254),
31 }
32 for _, v := range data {
33 err := binary.Write(buf, binary.LittleEndian, v)
34 if err != nil {
35 fmt.Println("binary.Write failed:", err)
36 }
37 }
38 fmt.Printf("%x", buf.Bytes())
39
40 }
41
42 func ExampleRead() {
43 var pi float64
44 b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
45 buf := bytes.NewReader(b)
46 err := binary.Read(buf, binary.LittleEndian, &pi)
47 if err != nil {
48 fmt.Println("binary.Read failed:", err)
49 }
50 fmt.Print(pi)
51
52 }
53
54 func ExampleRead_multi() {
55 b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef}
56 r := bytes.NewReader(b)
57
58 var data struct {
59 PI float64
60 Uate uint8
61 Mine [3]byte
62 Too uint16
63 }
64
65 if err := binary.Read(r, binary.LittleEndian, &data); err != nil {
66 fmt.Println("binary.Read failed:", err)
67 }
68
69 fmt.Println(data.PI)
70 fmt.Println(data.Uate)
71 fmt.Printf("% x\n", data.Mine)
72 fmt.Println(data.Too)
73
74
75
76
77
78 }
79
80 func ExampleByteOrder_put() {
81 b := make([]byte, 4)
82 binary.LittleEndian.PutUint16(b[0:], 0x03e8)
83 binary.LittleEndian.PutUint16(b[2:], 0x07d0)
84 fmt.Printf("% x\n", b)
85
86
87 }
88
89 func ExampleByteOrder_get() {
90 b := []byte{0xe8, 0x03, 0xd0, 0x07}
91 x1 := binary.LittleEndian.Uint16(b[0:])
92 x2 := binary.LittleEndian.Uint16(b[2:])
93 fmt.Printf("%#04x %#04x\n", x1, x2)
94
95
96 }
97
98 func ExamplePutUvarint() {
99 buf := make([]byte, binary.MaxVarintLen64)
100
101 for _, x := range []uint64{1, 2, 127, 128, 255, 256} {
102 n := binary.PutUvarint(buf, x)
103 fmt.Printf("%x\n", buf[:n])
104 }
105
106
107
108
109
110
111
112 }
113
114 func ExamplePutVarint() {
115 buf := make([]byte, binary.MaxVarintLen64)
116
117 for _, x := range []int64{-65, -64, -2, -1, 0, 1, 2, 63, 64} {
118 n := binary.PutVarint(buf, x)
119 fmt.Printf("%x\n", buf[:n])
120 }
121
122
123
124
125
126
127
128
129
130
131 }
132
133 func ExampleUvarint() {
134 inputs := [][]byte{
135 {0x01},
136 {0x02},
137 {0x7f},
138 {0x80, 0x01},
139 {0xff, 0x01},
140 {0x80, 0x02},
141 }
142 for _, b := range inputs {
143 x, n := binary.Uvarint(b)
144 if n != len(b) {
145 fmt.Println("Uvarint did not consume all of in")
146 }
147 fmt.Println(x)
148 }
149
150
151
152
153
154
155
156 }
157
158 func ExampleVarint() {
159 inputs := [][]byte{
160 {0x81, 0x01},
161 {0x7f},
162 {0x03},
163 {0x01},
164 {0x00},
165 {0x02},
166 {0x04},
167 {0x7e},
168 {0x80, 0x01},
169 }
170 for _, b := range inputs {
171 x, n := binary.Varint(b)
172 if n != len(b) {
173 fmt.Println("Varint did not consume all of in")
174 }
175 fmt.Println(x)
176 }
177
178
179
180
181
182
183
184
185
186
187 }
188
View as plain text