I can't seem to convert a bit string of 1s and 0s to a byte array.
Here is the code I have so far:
package main
import (
"strconv"
"encoding/binary"
"fmt"
)
func main() {
/* goal: convert a bit string (ex "10110110") to a byte array */
bitString := "00000000000000000000000100111000100001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000011"
bitNum, err := strconv.ParseUint(bitString, 2, 128) // turn my 128-bit bitstring into an int
if err != nil {
panic(err) // currently panics with "value out of range"
}
// convert my integer to a byte array
// code from https://stackoverflow.com/questions/16888357/convert-an-integer-to-a-byte-array
bs := make([]byte, 128) // allocate memory for my byte array
binary.LittleEndian.PutUint64(bs, bitNum) // convert my bitnum to a byte array
fmt.Println(bs)
}
I am clearly missing something, but I can't seem to convert a bit string of that size to a byte array.
edit I got passed the first error with this:
package main
import (
"fmt"
"strconv"
)
func main() {
/* goal: convert a bit string (ex "10110110") to a byte array */
bitString := "00000000000000000000000100111000100001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000011"
myBytes := make([]byte, 16)
for len(bitString) > 7 {
currentByteStr := bitString[:8]
bitString = bitString[8:]
currentByteInt, _ := strconv.ParseUint(currentByteStr, 2, 8)
currentByte := byte(currentByteInt)
myBytes = append(myBytes, currentByte)
}
fmt.Println(myBytes)
}
but it doesn't output what I would expect a byte array to look like:
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 56 134 0 0 0 0 0 0 0 0 0 0 3]
I would have expected it to be hex? is that not what a byte array looks like in golang?