-
Notifications
You must be signed in to change notification settings - Fork 985
/
parse_binary.go
53 lines (45 loc) · 1.18 KB
/
parse_binary.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 mysql
import (
"encoding/binary"
"math"
)
func ParseBinaryInt8(data []byte) int8 {
return int8(data[0])
}
func ParseBinaryUint8(data []byte) uint8 {
return data[0]
}
func ParseBinaryInt16(data []byte) int16 {
return int16(binary.LittleEndian.Uint16(data))
}
func ParseBinaryUint16(data []byte) uint16 {
return binary.LittleEndian.Uint16(data)
}
func ParseBinaryInt24(data []byte) int32 {
u32 := ParseBinaryUint24(data)
if u32&0x00800000 != 0 {
u32 |= 0xFF000000
}
return int32(u32)
}
func ParseBinaryUint24(data []byte) uint32 {
return uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16
}
func ParseBinaryInt32(data []byte) int32 {
return int32(binary.LittleEndian.Uint32(data))
}
func ParseBinaryUint32(data []byte) uint32 {
return binary.LittleEndian.Uint32(data)
}
func ParseBinaryInt64(data []byte) int64 {
return int64(binary.LittleEndian.Uint64(data))
}
func ParseBinaryUint64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}
func ParseBinaryFloat32(data []byte) float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(data))
}
func ParseBinaryFloat64(data []byte) float64 {
return math.Float64frombits(binary.LittleEndian.Uint64(data))
}