-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvert.go
More file actions
108 lines (96 loc) · 2.27 KB
/
Copy pathconvert.go
File metadata and controls
108 lines (96 loc) · 2.27 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* @Author: 时光弧线
* @Date: 2017-12-30 11:55:06
* @Last Modified by: 时光弧线
* @Last Modified time: 2017-12-30 11:55:06
*/
package tcplibrary
import (
"encoding/binary"
"math"
)
func ByteToBool(i byte) bool {
if i == 1 {
return true
}
return false
}
func BytesToUint16(data []byte) uint16 {
return binary.LittleEndian.Uint16(data)
}
func BytesToUint32(data []byte) uint32 {
return binary.LittleEndian.Uint32(data)
}
func BytesToUint64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}
func BytesToInt16(data []byte) int16 {
return int16(BytesToUint16(data))
}
func BytesToInt32(data []byte) int32 {
return int32(BytesToUint16(data))
}
func BytesToInt64(data []byte) int64 {
return int64(BytesToUint64(data))
}
func BytesToInt(data []byte) int {
switch len(data) {
case 2:
return int(BytesToUint16(data))
case 4:
return int(BytesToUint32(data))
case 8:
return int(BytesToUint64(data))
}
return 0
}
//IntToBytes 整形转换成byte数组
func IntToBytes(data interface{}) []byte {
var buf []byte
switch data.(type) {
case int:
buf = make([]byte, 8)
target, _ := data.(int)
binary.LittleEndian.PutUint64(buf, uint64(target))
case int16:
buf = make([]byte, 2)
target, _ := data.(int16)
binary.LittleEndian.PutUint16(buf, uint16(target))
case int32:
buf = make([]byte, 4)
target, _ := data.(int32)
binary.LittleEndian.PutUint32(buf, uint32(target))
case int64:
buf = make([]byte, 8)
target, _ := data.(int64)
binary.LittleEndian.PutUint64(buf, uint64(target))
case uint:
buf = make([]byte, 8)
target, _ := data.(uint)
binary.LittleEndian.PutUint64(buf, uint64(target))
case uint16:
buf = make([]byte, 2)
target, _ := data.(uint16)
binary.LittleEndian.PutUint16(buf, target)
case uint32:
buf = make([]byte, 4)
target, _ := data.(uint32)
binary.LittleEndian.PutUint32(buf, target)
case uint64:
buf = make([]byte, 8)
target, _ := data.(uint64)
binary.LittleEndian.PutUint64(buf, target)
}
return buf
}
func Float64frombytes(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}
func Float32bytes(float float32) []byte {
bits := math.Float32bits(float)
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, bits)
return bytes
}