-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryConversions.go
89 lines (81 loc) · 2.18 KB
/
binaryConversions.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
80
81
82
83
84
85
86
87
88
89
package main
//Check a string and return its bool slice
//FALSE = binary 0, TRUE = binary 1
func argumentToBinary(argument string) []bool {
stringLength := len(argument)
bits := make([]bool, stringLength)
for position, char := range argument {
if char == '0' {
bits[position] = false
} else if char == '1' {
bits[position] = true
}
}
return bits
}
func byteToBitSlice(bytes uint32, length uint8) []bool {
bits := make([]bool, length)
var i uint8
//The length of 32 means we're decoding a 32 bit sequence
//Loop through the and turn it into bit sequence using AND and masking
//Using an unsigned integer, so
//7 -> 0
for i = 0; i < length; i++ {
mask := uint32(1 << i)
if (bytes & uint32(mask)) > 0 {
bits[(length-1)-i] = true
} else {
bits[(length-1)-i] = false
}
}
return bits
}
//Takes a length*8 sized bool slice and turns it into a single byte, or 4 bytes if we're converting 32 byte length bitslice
func bitSliceToByte(bitSlice *[]bool, length uint8) []uint8 {
var i uint8 = 0
var j uint8 = 0
resultingBytes := make([]uint8, length)
for i = 0; i < length; i++ {
for j = 0; j < 8; j++ {
if (*bitSlice)[(i*8)+j] {
resultingBytes[i] |= 1 << (7 - j)
}
}
}
return resultingBytes
}
func sevenBitSliceToByte(bitSlice *[]bool, length uint8) []uint8 {
var i uint8 = 0
var j uint8 = 0
resultingBytes := make([]uint8, length)
for i = 0; i < length; i++ {
for j = 0; j < 7; j++ {
if (*bitSlice)[i+j] {
resultingBytes[i] |= 1 << (6 - j)
}
}
}
return resultingBytes
}
//takes 32 bits at a time to generate a byte out of it
func arbitraryBitsToByte(bitSlice *[]bool) uint32 {
var resultingByte uint32 = 0
var i uint8 = 0
length := len(*bitSlice)
for i = 0; i < uint8(length); i++ {
if (*bitSlice)[i] {
resultingByte |= 1 << ((uint8(length - 1)) - i)
}
}
return resultingByte
}
//Turn 4 bytes(uint8) into an uint32 by doing bit shifting magic
//Not done in a loop because it kept messing up for no reason.
func bytesToUint32(data []uint8) uint32 {
var returnValue uint32 = 0
returnValue |= uint32(data[3])
returnValue |= uint32(data[2]) << 8
returnValue |= uint32(data[1]) << 16
returnValue |= uint32(data[0]) << 24
return returnValue
}