2

I want to take in a byte [] and convert it to a bool []. I have found a code that places the bool in a bool queue, but I don't posses the necessary skill/knowledge to go from a method that gives me a bool queue to a method that gives me a bool array.

Any help is appreciated!

public void TmsTdiEnqueue(int bitCount, byte[] byteArray)
        {
            // New TAP bit queues with allocated number of bits
            boolQueue = new Queue<bool>(bitCount);

            // Fill bool queue
            int byteIndex = 0;
            int bitMask = 0x01;
            for (int i = 0; i < bitCount; i++)
            {
                boolQueue.Enqueue((tmsBytes[byteIndex] & bitMask) != 0);
                IncrementBitPointer(ref byteIndex, ref bitMask);
            }
        }

        private void IncrementBitPointer(ref int byteIndex, ref int bitMask)
        {
            byteIndex += (bitMask == 0x80) ? 1 : 0;
            bitMask = (bitMask == 0x80) ? 0x01 : bitMask << 1;
        }
2
  • What is the requirement regarding the elements of the bool array? When should be an element of it true and when should it be false? Commented Apr 22, 2020 at 14:23
  • Try following : Boolean[] status = byteArray.Select(x => (x == 0)? false : true).ToArray(); Commented Apr 22, 2020 at 14:30

2 Answers 2

6

If the source byte[] array has one boolean value per bit, you could simply use the BitArray class:

BitArray ba = new BitArray(new byte[] { 1, 2, 3 });
bool[] ret = new bool[ba.Length];
ba.CopyTo(ret, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

This works, however, the bytes are reversed. How can I reverse it back? If I expect a byte array with byte[0] = 28, byte[1] = 52, I get byte[0] = 52, byte[1] = 28
@Ioragi You could use the Array.Reverse static method.
1

You can use SelectMany to do this. First, start with a method that converts a single byte to an array of bool:

static bool[] ByteToBools(byte value)
{
    var values = new bool[8];

    values[0] = (value & 1) == 0 ? false : true;
    values[1] = (value & 2) == 0 ? false : true;
    values[2] = (value & 4) == 0 ? false : true;
    values[3] = (value & 8) == 0 ? false : true;
    values[4] = (value & 16) == 0 ? false : true;
    values[5] = (value & 32) == 0 ? false : true;
    values[6] = (value & 64) == 0 ? false : true;
    values[7] = (value & 128) == 0 ? false : true;

    return values;
}

Now we just SelectManyover thebyte array, and for each byte we call ByteToBools:

var numbers = new byte[]{1, 2, 3, 5, 255};

var bits = numbers.SelectMany(number => ByteToBools(number));
foreach(var bit in bits)
{
    Console.WriteLine(bit);
}

4 Comments

Anything of the form foo ? false : true can always be reduced to !foo. Therefore: values[0] = (value & 1) != 0 and so on.
This can also be improved using iterations, since your code follows the repeated format of values[i] = (value & Math.Pow(2,i)) != 0
@Flater - I disagree with the statement that iteration would improve the code. A loop with calls to Pow and then a compare will be more inefficient that a simple compare and assign.
You could use for (var i = 0, j = 1; i < 8; i++, j <<= 1) { values[i] = (value & j) != 0; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.