Here you can find the source of toByteArray(BitSet bitSet)
public static byte[] toByteArray(BitSet bitSet)
//package com.java2s; /*//from w w w . j av a 2 s . c o m * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006. * * Licensed under the Aduna BSD-style license. */ import java.util.BitSet; public class Main { public static byte[] toByteArray(BitSet bitSet) { byte[] array = new byte[bitSet.size() / 8 + 1]; for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { array[i / 8] |= byteMask(i); } return array; } private static byte byteMask(int bitNo) { return (byte) (0x80 >>> (bitNo % 8)); } }