Here you can find the source of toByteArray(BitSet bits)
public static byte[] toByteArray(BitSet bits)
//package com.java2s; import java.util.BitSet; public class Main { public static byte[] toByteArray(BitSet bits) { byte[] bytes = new byte[bits.size() / 8]; for (int i = 0; i < bits.length(); i++) { if (bits.get(i)) { bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8); }/*from w w w . j a v a 2 s . co m*/ } return bytes; } }