Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Distribution License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/org/documents/edl-v10.php. *******************************************************************************/ 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)); } }