-
Notifications
You must be signed in to change notification settings - Fork 9
/
codec.go
46 lines (36 loc) · 913 Bytes
/
codec.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
package codec
import "encoding/binary"
type Serializable interface {
Serialization(sink *ZeroCopySink)
}
func SerializeToBytes(values ...Serializable) []byte {
sink := NewZeroCopySink(nil)
for _, val := range values {
val.Serialization(sink)
}
return sink.Bytes()
}
func BytesLEToUint16(data []byte) uint16 {
return binary.LittleEndian.Uint16(data)
}
func Uint16ToBytesLE(val uint16) []byte {
data := make([]byte, 2)
binary.LittleEndian.PutUint16(data, val)
return data
}
func BytesLEToUint32(data []byte) uint32 {
return binary.LittleEndian.Uint32(data)
}
func Uint32ToBytesLE(val uint32) []byte {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, val)
return data
}
func BytesLEToUint64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}
func Uint64ToBytesLE(val uint64) []byte {
data := make([]byte, 8)
binary.LittleEndian.PutUint64(data, val)
return data
}