'how to convert bool array in one byte and later convert back in bool array

I would like to pack bool array with max length 8 in one byte, send it over network and then unpack it back to bool array. Tried some solutions here already but it didn't work. I'm using Mono.

I made BitArray and then tried to convert it in byte

public static byte[] BitArrayToByteArray(BitArray bits)
    {
      byte[] ret = new byte[Math.Max(1, bits.Length / 8)];
      bits.CopyTo(ret, 0);
      return ret;
    }

but I'm getting errors telling only int and long type can be used. Tried int instead of byte but same problem. I would like to avoid BitArray and use simple conversion from bool array to byte if possible



Solution 1:[1]

Here's how I would implement this.

To convert the bool[] to a byte:

private static byte ConvertBoolArrayToByte(bool[] source)
{
    byte result = 0;
    // This assumes the array never contains more than 8 elements!
    int index = 8 - source.Length;

    // Loop through the array
    foreach (bool b in source)
    {
        // if the element is 'true' set the bit at that position
        if (b)
            result |= (byte)(1 << (7 - index));

        index++;
    }

    return result;
}

To convert a byte to an array of bools with length 8:

private static bool[] ConvertByteToBoolArray(byte b)
{
    // prepare the return result
    bool[] result = new bool[8];

    // check each bit in the byte. if 1 set to true, if 0 set to false
    for (int i = 0; i < 8; i++)
        result[i] = (b & (1 << i)) != 0;

    // reverse the array
    Array.Reverse(result);

    return result;
}

Solution 2:[2]

Other static functions version

/// <summary>
/// Convert Byte Array To Bool Array
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static bool[] ConvertByteArrayToBoolArray(byte[] bytes)
{
    System.Collections.BitArray b = new System.Collections.BitArray(bytes);
    bool[] bitValues = new bool[b.Count];
    b.CopyTo(bitValues, 0);
    Array.Reverse(bitValues);
    return bitValues;
}

/// <summary>
/// Packs a bit array into bytes, most significant bit first
/// </summary>
/// <param name="boolArr"></param>
/// <returns></returns>
public static byte[] ConvertBoolArrayToByteArray(bool[] boolArr)
{
    byte[] byteArr = new byte[(boolArr.Length + 7) / 8];
    for (int i = 0; i < byteArr.Length; i++)
    {
        byteArr[i] = ReadByte(boolArr, 8 * i);
    }
    return byteArr;
}

Solution 3:[3]

Without Array.Reverse version :

public static void Byte2Flags(bool[] flags, byte range)
{
    if (flags == null || flags.Length < 8)
    {
        return;
    }

    for (int i = 0; i < 8; i++)
        flags[i] = (range & (1 << i)) > 0;
}

public static byte Flags2Byte(bool[] flags)
{
    byte range = 0;
    if (flags == null || flags.Length < 8)
    {
        range = 0;
    }

    for (int i = 0; i < 8; i++)
    {
        if (flags[i])
        {
            range |= (byte)(1 << i);
        }
    }

    return range;
}

And How to test:

    bool[] flags = new bool[8];
    byte b;

    for(int i = 0; i < 256; i++)
    {
        b = (byte)i;
        Byte2Flags(flags, b);
        byte back = Flags2Byte(flags);

        if(b != back)
        {
            //Not Match!
        }
    }

Solution 4:[4]

Here is a series of methods that do just that

using System;
using System.Text;                  
public class Program
{
    public void Main(){     
      byte _num = 1;
        Console.WriteLine(ReadAllBits(WriteBools(new bool[] {true,true,false,false,false,false,false,true})));
    }
    
    public string ReadAllBits(byte _mask){
        string _data = "";
        for(int i = 0; i < 8; i++){
            if((_mask& (1<<i)) !=0){
                _data += "1";
            }else{
                _data+="0";
            }
        }
        return _data;
    }
    
    public bool[] GetBools(byte _mask){
        bool[] _values = new bool[8];
        for(int i = 0; i < 8; i++){
            if((_mask& (1<<i)) !=0){
                _values[i] = true;
            }else{
                _values[i] = false;
            }
        }
        return _values;
    }
    
    public byte WriteBools(bool[] _data){
        byte _byte = 0;
            for(int i = 0; i < 8; i++){
                if(_data[i]){
                    _byte = System.Convert.ToByte(( _byte | (1<<i)));
                }
            }
        return _byte;
    }
}

you can find the full gist here https://gist.github.com/namusanga/abecca458a3a15ca9bfa6fcdd7953e31

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fabian Kamp
Solution 2 Alejandro Aranda
Solution 3 TimChang
Solution 4 Namusanga Nicholas