-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
89 lines (74 loc) · 1.57 KB
/
utils.go
File metadata and controls
89 lines (74 loc) · 1.57 KB
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
package xbeeapi
import (
"errors"
"fmt"
"strconv"
)
func concat(s []byte, others ...[]byte) []byte {
for _, o := range others {
if o != nil {
s = append(s, o...)
}
}
return s
}
func copySlice(s []byte) []byte {
if s == nil {
return nil
}
cpy := make([]byte, len(s))
copy(cpy, s)
return cpy
}
func hexToBytes(hex string) ([]byte, error) {
result := []byte{}
for _, h := range hex {
b, err := strconv.ParseUint(string(h), 16, 8)
if err != nil {
return nil, err
}
result = append(result, byte(b))
}
return result, nil
}
func bytesToHex(byteArray []byte) string {
hex := ""
for _, b := range byteArray {
hex += strconv.FormatUint(uint64(b), 16)
}
return hex
}
func address16(address string) ([]byte, error) {
a, err := hexToBytes(address)
if err != nil {
return nil, err
}
if len(a) != 4 {
return nil, errors.New(fmt.Sprintln("Expected 4 hex characters for address16 field:", address))
}
return a, err
}
func parseAddress16(address []byte) (string, error) {
b := bytesToHex(address)
if len(b) != 4 {
return "", errors.New(fmt.Sprintln("Expecting 16-bit address:", address))
}
return b, nil
}
func address64(address string) ([]byte, error) {
a, err := hexToBytes(address)
if err != nil {
return nil, err
}
if len(a) != 16 {
return nil, errors.New(fmt.Sprintln("Expected 16 hex characters for address64 field:", address))
}
return a, err
}
func parseAddress64(address []byte) (string, error) {
b := bytesToHex(address)
if len(b) != 16 {
return "", errors.New(fmt.Sprintln("Expecting 64-bit address:", address))
}
return b, nil
}