C#C
C#4y ago
Matt

Condense Byte[] to BitArray to smaller Byte[]

I have an array of 300 bytes, all either 0 or 1.
I need a function that takes in the byte array and performs a bitwise operation to convert the bytes to bits.
As an example:
If the first 8 bytes of the input array are
{ 01, 01, 00, 00, 00, 00, 00, 00 }

Then the BitArray should end up as
{ 00000011 }

Which ends up as a byte 03
Obviously the array will have to be handled in chunks of 8. The resulting byte array should be about 40 bytes long, hence condensing the 300 byte array to a 40 bytes array.
I can't deviate from this structure unfortunately, I wish I could but this is what is required. Any advice on anything I should read up on or a fancy linq statement that will do this all for me would be greatly appreciated. Thank you
Was this page helpful?