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
Then the BitArray should end up as
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
6 Replies
Loop up bit manipulation to get the bits you need in the second array
Mainly | and &
okay I'll start researching. Thanks
BitArray has a constructor that takes an array of ints so as long as your set is a multiple of 32 bits then you could use that to be efficient, combinining the bytes into ints
<< and >> are also useful for that
... It also has a constructor that takes an array of bytes so you could just cheese the whole thing
amazing. This I will definitely look into
lol okay I mean it's kinda useful that I was already working with ints. Might switch to bytes to make it easier. We'll see
Useful to learn bit manipulation while you're at it even if you don't end up using it
yes. The first element in the input byte array should be linked to the 1 bit (binary representation) in the BitArray
then the second to the 2 bit. The third to the 4 bit. etc
but I could just reverse each chunk of my byte array no?
built this as a test but it fails as index out of bounds on bits.CopyTo(outputBytes, i)
it's because feeding the BitArray a byte array it takes in all of the bits for the bytes
not sure how to convert from a byte of 00 to a bit of 0 and a byte of 01 to a bit of 1
This works but is horribly inefficient I imagine. Any ideas?