new BitArray(flag);
uses the BitArray(Int32 length)
constructor. This sets the capacity, not what you want.
You can use
new BitArray(new byte[] { flag });
Question
I am trying to read a byte
and convert into a bit
. I am trying to create a flagging system.
So byte 3 would be raise the first two flags as true. However When I try and convert it just passes the value of the byte
as the length of the BitArray
. Any help greatly appreciated.
BinaryReader br = new BinaryReader(filestream);
Byte flag = br.ReadByte();
BitArray flags = new BitArray(flag);
record.Isbool = flags[0];
record.Isbool1 = flags[1];
etc
Solution
new BitArray(flag);
uses the BitArray(Int32 length)
constructor. This sets the capacity, not what you want.
You can use
new BitArray(new byte[] { flag });
OTHER TIPS
You need to use
BitArray flags = new BitArray(new byte[] {flag});
The byte[]
overload uses the bits from the bytes; at the moment you're calling the int
overload which gives the length as you've discovered.
BinaryReader br = new BinaryReader(filestream);
Byte flag = br.ReadByte();
BitArray flags = new BitArray(new byte[] {flag});
record.Isbool = flags[0];
record.Isbool1 = flags[1];
Should work, by invoking the BitArray(byte[])
constructor.
Did you try to mask. int flag = value & 0x01 to mask the 1st bit.