-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexutils.go
46 lines (34 loc) · 1014 Bytes
/
hexutils.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
package hexutils
import (
"encoding/hex"
)
//Err contains information about the failure
type Err string
//Error returns printable string of the failure
func (meErr Err) Error() string {
return string(meErr)
}
const (
//ErrNotHex is error when the hex digit array contains non-hex digit
ErrNotHex = Err("Encountered non-hex digit!")
)
//ToUInt8 converts hex digits into uint8. Digits are assumed to be
//in big endian format.
func ToUInt8(aBytes [2]byte) (uint8, error) {
lDec := [1]byte{}
lNum, lErr := hex.Decode(lDec[:], aBytes[:])
if (lNum != len(lDec)) || (lErr != nil) {
return 0, ErrNotHex
}
return uint8(lDec[0]), nil
}
//ToUInt8 converts hex bytes into uint8. Digits are assumed to be
//in big endian format within bytes.
func ToUInt16(aBytes [4]byte) (uint16, error) {
lDec := [2]byte{}
lNum, lErr := hex.Decode(lDec[:], aBytes[:])
if (lNum != len(lDec)) || (lErr != nil) {
return 0, ErrNotHex
}
return MakeUInt16BE(lDec), nil
}