/
binaryutil.go
59 lines (52 loc) · 1.11 KB
/
binaryutil.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
package util
import (
"encoding/binary"
"strconv"
"strings"
)
const symbolicBitMask byte = 0x20 // 0010 0000
// BigEndianInt12 parse bit2-bit13
func BigEndianInt12(b []byte) int16 {
if len(b) < 2 {
return int16(b[0])
}
unsignedNumber := binary.BigEndian.Uint16(b[0:2])
unsignedNumber <<= 2
signedNumber := int16(unsignedNumber)
signedNumber >>= 4
return signedNumber
}
func BigEndianFromBit0ToBit6(b byte) int64 {
b <<= 1
b >>= 2
return int64(b)
}
func FromStrToUint32(str string) []byte {
value, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return nil
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(value))
return b
}
func FromDecStrToHexStr(str string) []byte {
value, err := strconv.ParseUint(str, 10, 16)
if err != nil {
return nil
}
return []byte(strings.ToUpper(strconv.FormatUint(value, 16)))
}
func GetEndBytes(input []byte, size int) []byte {
inputLen := len(input)
result := make([]byte, size)
for i := range result {
result[i] = '0'
}
if inputLen < size {
copy(result[size-inputLen:], input)
} else {
copy(result, input[inputLen-size:])
}
return result
}