-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
163 lines (132 loc) · 2.82 KB
/
conv.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sock
import (
"encoding/binary"
"errors"
"math"
)
func int2bytes(i int) []byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(i))
return b[:]
}
func bytes2int(b []byte) int {
return int(binary.BigEndian.Uint64(b))
}
func int82bytes(i int8) []byte {
var b [2]byte
binary.BigEndian.PutUint16(b[:], uint16(i))
return b[:]
}
func bytes2int8(b []byte) int8 {
return int8(binary.BigEndian.Uint16(b))
}
func int162bytes(i int16) []byte {
var b [2]byte
binary.BigEndian.PutUint16(b[:], uint16(i))
return b[:]
}
func bytes2int16(b []byte) int16 {
return int16(binary.BigEndian.Uint16(b))
}
func int322bytes(i int32) []byte {
var b [4]byte
binary.BigEndian.PutUint32(b[:], uint32(i))
return b[:]
}
func bytes2int32(b []byte) int32 {
return int32(binary.BigEndian.Uint32(b))
}
func int642bytes(i int64) []byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(i))
return b[:]
}
func bytes2int64(b []byte) int64 {
return int64(binary.BigEndian.Uint64(b))
}
func uint2bytes(u uint) []byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(u))
return b[:]
}
func bytes2uint(b []byte) uint {
return uint(binary.BigEndian.Uint64(b))
}
func uint82bytes(u uint8) []byte {
var b [2]byte
binary.BigEndian.PutUint16(b[:], uint16(u))
return b[:]
}
func bytes2uint8(b []byte) uint8 {
return uint8(binary.BigEndian.Uint16(b))
}
func uint162bytes(u uint16) []byte {
var b [2]byte
binary.BigEndian.PutUint16(b[:], u)
return b[:]
}
func bytes2uint16(b []byte) uint16 {
return binary.BigEndian.Uint16(b)
}
func uint322bytes(u uint32) []byte {
var b [4]byte
binary.BigEndian.PutUint32(b[:], u)
return b[:]
}
func bytes2uint32(b []byte) uint32 {
return binary.BigEndian.Uint32(b)
}
func uint642bytes(u uint64) []byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], u)
return b[:]
}
func bytes2uint64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
func bool2bytes(b bool) []byte {
if b {
return []byte{1}
}
return []byte{0}
}
func bytes2bool(b []byte) bool {
if len(b) > 0 && b[0] == 1 {
return true
}
return false
}
func error2bytes(e error) []byte {
if e == nil {
return nil
}
return []byte(e.Error())
}
func bytes2error(b []byte) error {
if len(b) == 0 {
return nil
}
return errors.New(string(b))
}
func rune2bytes(r rune) []byte {
return []byte(string([]rune{r}))
}
func bytes2rune(b []byte) rune {
return []rune(string(b))[0]
}
func float642bytes(f float64) []byte {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(f))
return buf[:]
}
func bytes2float64(b []byte) float64 {
return math.Float64frombits(binary.BigEndian.Uint64(b))
}
func float322bytes(f float32) []byte {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], math.Float32bits(f))
return buf[:]
}
func bytes2float32(b []byte) float32 {
return math.Float32frombits(binary.BigEndian.Uint32(b))
}