-
Notifications
You must be signed in to change notification settings - Fork 0
/
byteconverter.go
198 lines (169 loc) · 4.54 KB
/
byteconverter.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
/* Copyright (C) 2018-2020 Erik Fleckstein <erik@tinkerforge.com>
*
* Redistribution and use in source and binary forms of this file,
* with or without modification, are permitted. See the Creative
* Commons Zero (CC0 1.0) License for more details.
*/
package internal
import (
"bytes"
"encoding/binary"
"fmt"
)
func Min(left, right int64) int64 {
if left < right {
return left
}
return right
}
func MinU(left, right uint64) uint64 {
if left < right {
return left
}
return right
}
func BoolSliceToByteSlice(b []bool) []byte {
result := make([]byte, len(b)/8)
for i := range result {
for bit := 0; bit < 8; bit++ {
if b[i*8+bit] {
result[i] |= 1 << uint(bit)
}
}
}
return result
}
func ByteSliceToBoolSlice(b []byte) []bool {
result := make([]bool, len(b)*8)
for i := range result {
result[i] = b[i/8]&(1<<uint(i%8)) != 0
}
return result
}
func FixedSliceToByteSlice(slice []interface{}) []byte {
var buf bytes.Buffer
for _, item := range slice {
binary.Write(&buf, binary.LittleEndian, item)
}
return buf.Bytes()
}
func ByteSliceToInt8Slice(slice []byte) []int8 {
buf := bytes.NewBuffer(slice)
result := make([]int8, len(slice))
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToUint8Slice(slice []byte) []uint8 {
buf := bytes.NewBuffer(slice)
result := make([]uint8, len(slice))
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func Uint8SliceToByteSlice(slice []uint8) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, slice)
return buf.Bytes()
}
func Uint64SliceToByteSlice(slice []uint64) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, slice)
return buf.Bytes()
}
func Uint16SliceToByteSlice(slice []uint16) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, slice)
return buf.Bytes()
}
func Int16SliceToByteSlice(slice []int16) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, slice)
return buf.Bytes()
}
func Int8SliceToByteSlice(slice []int8) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, slice)
return buf.Bytes()
}
func ByteSliceToInt16Slice(slice []byte) []int16 {
buf := bytes.NewBuffer(slice)
result := make([]int16, len(slice)/2)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToUint16Slice(slice []byte) []uint16 {
buf := bytes.NewBuffer(slice)
result := make([]uint16, len(slice)/2)
err := binary.Read(buf, binary.LittleEndian, result)
if err != nil {
panic(err)
}
return result
}
func ByteSliceToInt32Slice(slice []byte) []int32 {
buf := bytes.NewBuffer(slice)
result := make([]int32, len(slice)/4)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToUint32Slice(slice []byte) []uint32 {
buf := bytes.NewBuffer(slice)
result := make([]uint32, len(slice)/4)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToInt64Slice(slice []byte) []int64 {
buf := bytes.NewBuffer(slice)
result := make([]int64, len(slice)/8)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToUint64Slice(slice []byte) []uint64 {
buf := bytes.NewBuffer(slice)
result := make([]uint64, len(slice)/8)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToFloat32Slice(slice []byte) []float32 {
buf := bytes.NewBuffer(slice)
result := make([]float32, len(slice)/4)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToFloat64Slice(slice []byte) []float64 {
buf := bytes.NewBuffer(slice)
result := make([]float64, len(slice)/8)
binary.Read(buf, binary.LittleEndian, result[:])
return result
}
func ByteSliceToRuneSlice(slice []byte) []rune {
result := make([]rune, len(slice))
for idx, b := range slice {
result[idx] = rune(b)
}
return result
}
func RuneSliceToByteSlice(slice []rune) []byte {
result := make([]byte, len(slice))
for idx, b := range slice {
result[idx] = byte(b)
}
return result
}
func ByteSliceToString(slice []byte) string {
n := bytes.IndexByte(slice, 0)
return string(slice[:n])
}
func StringToByteSlice(str string, maxLen uint64) ([]byte, error) {
runes := []rune(str)
bytes := make([]byte, maxLen, maxLen)
if uint64(len(runes)) > maxLen {
return nil, fmt.Errorf("invalid parameter: string is %d runes long, but only %d are allowed", len(runes), maxLen)
}
for i, r := range runes {
if int32(r) > 255 {
return nil, fmt.Errorf("Invalid parameter: rune %c can not be encoded, as it is longer than one byte", r)
}
bytes[i] = byte(r)
}
return bytes, nil
}