4

Ok, so i have a byte[] that i get using File.ReadAllBytes(filename); My problem is that my program needs to treat the data from the file as an array of bools. I have searched but i didn't manage to find a way to get a correct and efficient conversion. An example would be:

{ 00101011, 10111010 } ->
            { false, false, true, false, true, false, false, true, true,
              false, true, true, true, false, true, false }

I will also be needing to reverse the procedure.

Most Solutions i came across involved getting one boolean out of each byte. i.e, the resulting array of bool[] had the same length as the byte[] array, i don't seem to understand how this is possible, how do 8 bits result in only one boolean value? In my case i need to have a resulting array as: bool[bytes.Length * 8].

Thanks alot, any help is highly appreciated.

Implementing one of the solutions i tryed to get this to work but it is somehow wrong because the resulting file, which is a copy of the file i read gets damaged:

public static bool[] boolsFromFile(string filename)
    {
        List<bool> b = new List<bool>();
        using (FileStream fileStream = new FileStream(filename, FileMode.Open))
        using (BinaryReader read = new BinaryReader(fileStream))
        {
            while (fileStream.Position != fileStream.Length)
                b.Add(read.ReadBoolean());
        }
        return b.ToArray();
    }

    public static void boolsToFile(string filename, bool[] bools)
    {
        using (FileStream fileStream = new FileStream(filename, FileMode.Create))
        using (BinaryWriter write = new BinaryWriter(fileStream))
        {
            foreach (bool b in bools)
                write.Write(b);
        }
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Try using [bitwise operations](http://www.tutorialspoint.com/csharp/csharp_bitwise_operators.htm). – Archimaredes Jan 23 '16 at 11:39
  • How is the file made in the first place. If the file is made with a BinaryWriter it would be obvious to read the file with BinaryReader. – Esben Skov Pedersen Jan 23 '16 at 11:43
  • 1
    @Esben suggesting to read the file the same way as it's written is a good one, however the BinaryWriter [writes bools as bytes as well](http://stackoverflow.com/questions/10057747/write-one-single-bit-to-binary-file-using-binarywriter). – CodeCaster Jan 23 '16 at 11:51
  • @CodeCaster Yest i tried this and i think it is my best chance. I Managed to read a file and rewrite a new copy, the new file i write has exactly the same size but is somehow damaged. Could i be missing something? – Krishanmarco Jan 23 '16 at 12:45

2 Answers2

6

A .NET bool "wastes" seven bits. So there is no direct way to go from a byte to eight booleans.

You could use the BitArray class, see Converting C# byte to BitArray.

So something like this:

var bytes = File.ReadAllBytes(filename);
var bitArray = new BitArray(bytes);
bool ninthBit = bitArray[8];
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Easy with Linq

            byte[] input = new byte[] { 0x2b, 0xba };
            Boolean[][] results = input.Select(x => Enumerable.Repeat(x,8).Select((y, i) => ((y >> (7 - i)) & 1) == 0 ? false : true).ToArray()).ToArray();
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I would say it might be easy, but it certainly isn't readable. He's looking for a BitMask.@CodeCaster's solution seems more fitting and unproportionately more readable. – Giora Guttsait Jan 23 '16 at 13:50
  • Don't guess what the person wants in this case. If he is looking for a bit mask then there is absolutely no reason to do any conversion. A byte is a mask. – jdweng Jan 23 '16 at 13:57
  • Right, but why go through a complicated linq when there's something a lot more simple to work with? – Giora Guttsait Jan 23 '16 at 14:06
  • I can't answer your question only the person who wrote the question can. He said he needed an array of Boolean so I gave him one. – jdweng Jan 23 '16 at 14:26