How can I convert bits to bytes?

The code is treating the first bit as the low bit of the word, so you end up with each word reversed. As a quick-and-dirty fix, try this:

bytes[byteIndex] |= (byte)(1 << (7-bitIndex));

That puts the first bit in the array at the highest position in the first byte, etc.

Leave a Comment