summaryrefslogtreecommitdiff
path: root/utils/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/utils.go')
-rw-r--r--utils/utils.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/utils/utils.go b/utils/utils.go
new file mode 100644
index 0000000..4e16ba3
--- /dev/null
+++ b/utils/utils.go
@@ -0,0 +1,43 @@
+package utils
+
+import (
+ "fmt"
+)
+
+// BytesToInt converts a slice of bytes to an int. Only 8- and 16-bit uints are
+// supported, because those are the only thing the control tables contain.
+func BytesToInt(b []byte) (int, error) {
+
+ switch len(b) {
+ case 1:
+ return int(b[0]), nil
+
+ case 2:
+ return int(b[0]) | int(b[1])<<8, nil
+
+ default:
+ return 0, fmt.Errorf("invalid read length %d", len(b))
+
+ }
+}
+
+// BoolToInt converts a bool to an int.
+func BoolToInt(b bool) int {
+ if b {
+ return 1
+ }
+ return 0
+}
+
+// IntToBool converts an int to a bool.
+func IntToBool(v int) bool {
+ return (v != 0)
+}
+
+func Low(i uint16) byte {
+ return byte(i & 0xFF)
+}
+
+func High(i uint16) byte {
+ return Low(i >> 8)
+}