-
Notifications
You must be signed in to change notification settings - Fork 89
/
encoding.go
209 lines (164 loc) · 4.6 KB
/
encoding.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package modbus
import (
"encoding/binary"
"math"
)
func uint16ToBytes(endianness Endianness, in uint16) (out []byte) {
out = make([]byte, 2)
switch endianness {
case BIG_ENDIAN: binary.BigEndian.PutUint16(out, in)
case LITTLE_ENDIAN: binary.LittleEndian.PutUint16(out, in)
}
return
}
func uint16sToBytes(endianness Endianness, in []uint16) (out []byte) {
for i := range in {
out = append(out, uint16ToBytes(endianness, in[i])...)
}
return
}
func bytesToUint16(endianness Endianness, in []byte) (out uint16) {
switch endianness {
case BIG_ENDIAN: out = binary.BigEndian.Uint16(in)
case LITTLE_ENDIAN: out = binary.LittleEndian.Uint16(in)
}
return
}
func bytesToUint16s(endianness Endianness, in []byte) (out []uint16) {
for i := 0; i < len(in); i += 2 {
out = append(out, bytesToUint16(endianness, in[i:i+2]))
}
return
}
func bytesToUint32s(endianness Endianness, wordOrder WordOrder, in []byte) (out []uint32) {
var u32 uint32
for i := 0; i < len(in); i += 4 {
switch endianness {
case BIG_ENDIAN:
if wordOrder == HIGH_WORD_FIRST {
u32 = binary.BigEndian.Uint32(in[i:i+4])
} else {
u32 = binary.BigEndian.Uint32(
[]byte{in[i+2], in[i+3], in[i+0], in[i+1]})
}
case LITTLE_ENDIAN:
if wordOrder == LOW_WORD_FIRST {
u32 = binary.LittleEndian.Uint32(in[i:i+4])
} else {
u32 = binary.LittleEndian.Uint32(
[]byte{in[i+2], in[i+3], in[i+0], in[i+1]})
}
}
out = append(out, u32)
}
return
}
func uint32ToBytes(endianness Endianness, wordOrder WordOrder, in uint32) (out []byte) {
out = make([]byte, 4)
switch endianness {
case BIG_ENDIAN:
binary.BigEndian.PutUint32(out, in)
// swap words if needed
if wordOrder == LOW_WORD_FIRST {
out[0], out[1], out[2], out[3] = out[2], out[3], out[0], out[1]
}
case LITTLE_ENDIAN:
binary.LittleEndian.PutUint32(out, in)
// swap words if needed
if wordOrder == HIGH_WORD_FIRST {
out[0], out[1], out[2], out[3] = out[2], out[3], out[0], out[1]
}
}
return
}
func bytesToFloat32s(endianness Endianness, wordOrder WordOrder, in []byte) (out []float32) {
var u32s []uint32
u32s = bytesToUint32s(endianness, wordOrder, in)
for _, u32 := range u32s {
out = append(out, math.Float32frombits(u32))
}
return
}
func float32ToBytes(endianness Endianness, wordOrder WordOrder, in float32) (out []byte) {
out = uint32ToBytes(endianness, wordOrder, math.Float32bits(in))
return
}
func bytesToUint64s(endianness Endianness, wordOrder WordOrder, in []byte) (out []uint64) {
var u64 uint64
for i := 0; i < len(in); i += 8 {
switch endianness {
case BIG_ENDIAN:
if wordOrder == HIGH_WORD_FIRST {
u64 = binary.BigEndian.Uint64(in[i:i+8])
} else {
u64 = binary.BigEndian.Uint64(
[]byte{in[i+6], in[i+7], in[i+4], in[i+5],
in[i+2], in[i+3], in[i+0], in[i+1]})
}
case LITTLE_ENDIAN:
if wordOrder == LOW_WORD_FIRST {
u64 = binary.LittleEndian.Uint64(in[i:i+8])
} else {
u64 = binary.LittleEndian.Uint64(
[]byte{in[i+6], in[i+7], in[i+4], in[i+5],
in[i+2], in[i+3], in[i+0], in[i+1]})
}
}
out = append(out, u64)
}
return
}
func uint64ToBytes(endianness Endianness, wordOrder WordOrder, in uint64) (out []byte) {
out = make([]byte, 8)
switch endianness {
case BIG_ENDIAN:
binary.BigEndian.PutUint64(out, in)
// swap words if needed
if wordOrder == LOW_WORD_FIRST {
out[0], out[1], out[2], out[3],out[4], out[5], out[6], out[7] =
out[6], out[7], out[4], out[5], out[2], out[3], out[0], out[1]
}
case LITTLE_ENDIAN:
binary.LittleEndian.PutUint64(out, in)
// swap words if needed
if wordOrder == HIGH_WORD_FIRST {
out[0], out[1], out[2], out[3],out[4], out[5], out[6], out[7] =
out[6], out[7], out[4], out[5], out[2], out[3], out[0], out[1]
}
}
return
}
func bytesToFloat64s(endianness Endianness, wordOrder WordOrder, in []byte) (out []float64) {
var u64s []uint64
u64s = bytesToUint64s(endianness, wordOrder, in)
for _, u64 := range u64s {
out = append(out, math.Float64frombits(u64))
}
return
}
func float64ToBytes(endianness Endianness, wordOrder WordOrder, in float64) (out []byte) {
out = uint64ToBytes(endianness, wordOrder, math.Float64bits(in))
return
}
func encodeBools(in []bool) (out []byte) {
var byteCount uint
var i uint
byteCount = uint(len(in)) / 8
if len(in) % 8 != 0 {
byteCount++
}
out = make([]byte, byteCount)
for i = 0; i < uint(len(in)); i++ {
if in[i] {
out[i/8] |= (0x01 << (i % 8))
}
}
return
}
func decodeBools(quantity uint16, in []byte) (out []bool) {
var i uint
for i = 0; i < uint(quantity); i++ {
out = append(out, (((in[i/8] >> (i % 8)) & 0x01) == 0x01))
}
return
}