-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.go
57 lines (48 loc) · 970 Bytes
/
utils.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
54
55
56
57
package main
import (
"encoding/binary"
)
/*----------------------------------------------
*
* 基本类型编码成字节数组
*
----------------------------------------------*/
func Int32ToBytes(buf []byte, i int32) {
binary.BigEndian.PutUint32(buf, uint32(i))
}
func Int64ToBytes(buf []byte, i int64) {
binary.BigEndian.PutUint64(buf, uint64(i))
}
func BoolToBytes(buf []byte, b bool) {
if b {
buf[0] = 1
} else {
buf[0] = 0
}
}
/*----------------------------------------------
*
* 节点数组解码成基本类型
*
----------------------------------------------*/
func BytesToInt32(buf []byte) int32 {
if len(buf) < 4 {
return int32(0)
}
return int32(binary.BigEndian.Uint32(buf))
}
func BytesToInt64(buf []byte) int64 {
if len(buf) < 8 {
return int64(0)
}
return int64(binary.BigEndian.Uint64(buf))
}
func BytesToBool(buf []byte) bool {
if len(buf) < 0 {
return false
}
if buf[0] == 1 {
return true
}
return false
}