-
Notifications
You must be signed in to change notification settings - Fork 8
/
bytesutil.go
53 lines (46 loc) · 1.09 KB
/
bytesutil.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
package bytesutil
import (
"bytes"
"github.com/grokify/simplego/fmt/fmtutil"
"github.com/grokify/simplego/math/mathutil"
)
const UTF8BOM = "\xef\xbb\xbf"
func TrimUTF8BOM(data []byte) []byte {
return bytes.TrimPrefix(data, []byte(UTF8BOM))
}
func BytesToInt(s []byte) int {
var res int
for _, v := range s {
res <<= 8
res |= int(v)
}
return res
}
// https://stackoverflow.com/questions/48178008/convert-byte-slice-to-int-slice
func BytesToInts(bytes []byte) []int {
ints := []int{}
for _, b := range bytes {
ints = append(ints, int(b))
}
return ints
}
// https://stackoverflow.com/questions/48178008/convert-byte-slice-to-int-slice
func BytesToIntsMore(bytes []byte, intLength int) []int {
//ints := make([]int, len(bytes))
ints := []int{}
curNum := []byte{}
for i, b := range bytes {
curNum = append(curNum, b)
if intLength > 0 {
if mod := mathutil.ModInt(i, intLength); mod == intLength-1 {
ints = append(ints, BytesToInt(curNum))
curNum = []byte{}
}
}
}
if len(curNum) > 0 {
ints = append(ints, BytesToInt(curNum))
}
fmtutil.PrintJSON(ints)
return ints
}