2

I am trying to convert a bool array of length 8 into a byte. Anyone know how?

mei := [8]bool{true, true, true, true, false, false, false, false}
myvar := ConvertToByte(mei)
BAR
  • 15,909
  • 27
  • 97
  • 185
  • AFAIK there's no simple way to do this in Go. So you'll need to write your own implementation. Here are some questions about the same thing in [Java](https://stackoverflow.com/questions/32048064/java-simple-boolean-to-byte-conversion) and [C#](https://stackoverflow.com/questions/24322417/how-to-convert-bool-array-in-one-byte-and-later-convert-back-in-bool-array) which might help you get started. – phonaputer Sep 13 '22 at 23:55
  • What is the intended value of `myvar` in your example? – Hymns For Disco Sep 14 '22 at 00:01
  • @HymnsForDisco Changed the goal post, sorry about that! – BAR Sep 14 '22 at 00:25
  • 1
    `byte` is simply an alias for `uint8`; they are the same type. – Hymns For Disco Sep 14 '22 at 00:30

3 Answers3

2

Iterate through the bits, shifting and setting as you go.

Here's the code for the case where the most significant bit is at index 0 in the array:

func ConvertToUint8(mei [8]bool) uint8 {
    var result uint8
    for _, b := range mei {
        result <<= 1
        if b {
            result |= 1
        }
    }
    return result
}


mei := [8]bool{true, true, true, true, false, false, false, false}
myvar := ConvertToUint8(mei)
fmt.Printf("%b\n", myvar) // prints 11110000

Here's the code for the case where the least significant bit is at index 0 in the array:

func ConvertToUint8(mei [8]bool) uint8 {
    var result uint8
    for _, b := range mei {
        result >>= 1
        if b {
            result |= 0b10000000
        }
    }
    return result
}

mei := [8]bool{true, true, true, true, false, false, false, false}
myvar := ConvertToUint8(mei)
fmt.Printf("%08b\n", myvar) // prints 00001111
malificent
  • 36
  • 2
  • For converting to a uint8, is it the same thing but with `byte` replaced by `uint8`? – BAR Sep 14 '22 at 00:32
  • @BAR [byte](https://pkg.go.dev/builtin#byte) is an [alias](https://go.dev/ref/spec#Alias_declarations) for uint8. Use either type. I edit answer to reduce confusion. – malificent Sep 14 '22 at 01:20
2
func ConvertToByte(bits [8]bool) byte {
    var b byte
    for _, bit := range bits {
        b <<= 1
        if bit {
            b |= 1
        }
    }
    return b
}
Hymns For Disco
  • 7,530
  • 2
  • 17
  • 33
  • For converting to a uint8, is it the same thing but with byte replaced by uint8? – BAR Sep 14 '22 at 00:33
  • @BAR See my other comment, but `byte` and `uint8` are just 2 words to refer to the exact same type in Go. You can replace any or all occurrences of `byte` with `uint8` or vice versa and it will be the same program. Use whichever one you prefer. – Hymns For Disco Sep 14 '22 at 01:04
-1

Just a little bit-twiddling. Iterate over the array:

func boolsToByte( flags [8]bool ) (b byte) {
    mask := byte(0x80)
    for _, f := range flags {
        if f {
            b |= mask
        }
        mask >>= 1
    }
    return b
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135