How can i convert the following bit array to a byte array?
int[] bits = new int[]{1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
The bits should be mapped to bytes as follows:
1010110101 -> 10101101 01000000 (0xAD 0x40)
How can i convert the following bit array to a byte array?
int[] bits = new int[]{1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
The bits should be mapped to bytes as follows:
1010110101 -> 10101101 01000000 (0xAD 0x40)
public class TestBitToByteEncoder {
public static void main(String[] args) {
int[] bits = new int[]{1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
byte[] bytes = encodeToByteArray(bits);
}
private static byte[] encodeToByteArray(int[] bits) {
byte[] results = new byte[(bits.length + 7) / 8];
int byteValue = 0;
int index;
for (index = 0; index < bits.length; index++) {
byteValue = (byteValue << 1) | bits[index];
if (index %8 == 7) {
results[index / 8] = (byte) byteValue;
}
}
if (index % 8 != 0) {
results[index / 8] = (byte) byteValue << (8 - (index % 8));
}
return results;
}
}
Edit: If you are happy to store the bits in the opposite order within bytes:
private static byte[] encodeToByteArray(int[] bits) {
BitSet bitSet = new BitSet(bits.length);
for (int index = 0; index < bits.length; index++) {
bitSet.set(index, bits[index] > 0);
}
return bitSet.toByteArray();
}