-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathints.go
More file actions
41 lines (33 loc) · 914 Bytes
/
ints.go
File metadata and controls
41 lines (33 loc) · 914 Bytes
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
package binenc
import "encoding/binary"
// UintEncoder allows encoding uint values directly to byte slices.
type UintEncoder interface {
binary.ByteOrder
EncodeUint16(x uint16) []byte
EncodeUint32(x uint32) []byte
EncodeUint64(x uint64) []byte
}
var (
// BigEndian provides encoding functions for the big endian byte order.
BigEndian = uintEncoder{ByteOrder: binary.BigEndian}
// LittleEndian provides encoding functions for the little endian byte order.
LittleEndian = uintEncoder{ByteOrder: binary.LittleEndian}
)
type uintEncoder struct {
binary.ByteOrder
}
func (e uintEncoder) EncodeUint16(x uint16) []byte {
buf := make([]byte, 2)
e.PutUint16(buf, x)
return buf
}
func (e uintEncoder) EncodeUint32(x uint32) []byte {
buf := make([]byte, 4)
e.PutUint32(buf, x)
return buf
}
func (e uintEncoder) EncodeUint64(x uint64) []byte {
buf := make([]byte, 8)
e.PutUint64(buf, x)
return buf
}