-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigEndian.go
53 lines (47 loc) · 1.24 KB
/
bigEndian.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
package main
func decodeUint16(data []byte, offset int) uint16 {
return uint16(
data[offset]<<8 |
data[offset+1])
}
func encodeUint16(value uint16, data []byte, offset int) []byte {
data[offset] = (byte)(value >> 8)
data[offset+1] = (byte)(value)
return data
}
func decodeUint32(data []byte, offset int) uint32 {
return uint32(
data[offset+0]<<24 |
data[offset+1]<<16 |
data[offset+2]<<8 |
data[offset+3])
}
func encodeUint32(value uint32, data []byte, offset int) []byte {
data[offset] = byte(value >> 24)
data[offset+1] = byte(value >> 16)
data[offset+2] = byte(value >> 8)
data[offset+3] = byte(value)
return data
}
func decodeUint64(data []byte, offset int) uint64 {
return uint64(
data[offset+0]<<56 |
data[offset+1]<<48 |
data[offset+2]<<40 |
data[offset+3]<<32 |
data[offset+4]<<24 |
data[offset+5]<<16 |
data[offset+6]<<8 |
data[offset+7])
}
func encodeUint64(value uint64, data []byte, offset int) []byte {
data[offset] = byte(value >> 56)
data[offset+1] = byte(value >> 48)
data[offset+2] = byte(value >> 40)
data[offset+3] = byte(value >> 32)
data[offset+4] = byte(value >> 24)
data[offset+5] = byte(value >> 16)
data[offset+6] = byte(value >> 8)
data[offset+7] = byte(value)
return data
}