-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.go
79 lines (66 loc) · 1.4 KB
/
convert.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
package main
import (
"encoding/binary"
"encoding/hex"
"strconv"
"strings"
)
// uint64 => string
func DecToHexstring(hex_num uint64) string {
return strconv.FormatInt(int64(hex_num), 16)
}
// string => string
func BinstringToString(byteArr string) string {
byteArr = strings.ReplaceAll(byteArr, ",", "")
byteArr = strings.ReplaceAll(byteArr, "00", "")
data, err := hex.DecodeString(byteArr)
if err != nil {
panic(err)
}
return string(data)
}
// string => []byte
func BinStringToByteArray(byteArr string) []byte {
byteArr = strings.ReplaceAll(byteArr, ",", "")
data, err := hex.DecodeString(byteArr)
if err != nil {
panic(err)
}
return data
}
// []byte => string
func ByteArrayToBinString(byteArr []byte) string {
data := hex.EncodeToString(byteArr)
var result string
var i = 0
for _, char := range data {
if i%2 == 0 {
result += ","
}
result += string(char)
i++
}
if result[0] == 44 { // Comma found
return result[1:]
}
return result
}
// []byte => uint64
func ByteArrayToUint64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}
// uint64 => []byte
func Uint64ToByteArray(data uint64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, data)
return b
}
// CompareInteger
func CompareDWord(regValue, fileValue string) bool {
switch {
case (regValue == "0" && fileValue == "") || regValue == fileValue:
return true
default:
return false
}
}