-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytransforms.go
More file actions
97 lines (87 loc) · 2.68 KB
/
binarytransforms.go
File metadata and controls
97 lines (87 loc) · 2.68 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
90
91
92
93
94
95
96
97
// Copyright (c) 2020 Alec Randazzo
package binarytransforms
import (
"encoding/binary"
"errors"
"fmt"
"strings"
)
// LittleEndianBinaryToInt64 converts a little endian byte slice to int64.
func LittleEndianBinaryToInt64(inBytes []byte) (outInt64 int64, err error) {
inBytesLength := len(inBytes)
if inBytesLength > 8 || inBytesLength == 0 {
err = fmt.Errorf("LittleEndianBinaryToInt64() received %d bytes but expected 1-8 bytes", inBytesLength)
outInt64 = 0
return
}
// Pad it to get to 8 bytes
if inBytes[inBytesLength-1] >= 0x80 {
if inBytesLength < 8 {
bytesToPad := 8 - inBytesLength
for i := 0; i < bytesToPad; i++ {
inBytes = append(inBytes, 0xff)
}
}
} else {
if inBytesLength < 8 {
bytesToPad := 8 - inBytesLength
for i := 0; i < bytesToPad; i++ {
inBytes = append(inBytes, 0x00)
}
}
}
outInt64 = int64(binary.LittleEndian.Uint64(inBytes))
return
}
// LittleEndianBinaryToUInt64 converts a little endian byte slice to uint64.
func LittleEndianBinaryToUInt64(inBytes []byte) (outUInt64 uint64, err error) {
inBytesLength := len(inBytes)
if inBytesLength > 8 || inBytesLength == 0 {
err = fmt.Errorf("LittleEndianBinaryToUInt64() received %d bytes but expected 1-8 bytes", inBytesLength)
outUInt64 = 0
return
}
// Pad it to get to 8 bytes
if inBytesLength < 8 {
bytesToPad := 8 - inBytesLength
for i := 0; i < bytesToPad; i++ {
inBytes = append(inBytes, 0x00)
}
}
outUInt64 = binary.LittleEndian.Uint64(inBytes)
return
}
// LittleEndianBinaryToUInt16 converts a little endian byte slice to uint16.
func LittleEndianBinaryToUInt16(inBytes []byte) (outUInt16 uint16, err error) {
inBytesLength := len(inBytes)
if inBytesLength != 2 {
err = fmt.Errorf("LittleEndianBinaryToUInt16() received %d bytes but expected 2 bytes", inBytesLength)
outUInt16 = 0
return
}
outUInt16 = binary.LittleEndian.Uint16(inBytes)
return
}
// LittleEndianBinaryToUInt32 converts a little endian byte slice to uint32.
func LittleEndianBinaryToUInt32(inBytes []byte) (outUInt32 uint32, err error) {
inBytesLength := len(inBytes)
if inBytesLength != 4 {
err = fmt.Errorf("LittleEndianBinaryToUInt32() received %d bytes but expected 4 bytes", inBytesLength)
outUInt32 = 0
return
}
outUInt32 = binary.LittleEndian.Uint32(inBytes)
return
}
// UnicodeBytesToASCII converts a byte slice of unicode characters to ASCII
func UnicodeBytesToASCII(unicodeBytes []byte) (asciiString string, err error) {
inBytesLength := len(unicodeBytes)
if inBytesLength == 0 {
err = errors.New("UnicodeBytesToASCII() received no bytes")
asciiString = ""
return
}
unicodeString := string(unicodeBytes)
asciiString = strings.Replace(unicodeString, "\x00", "", -1)
return
}