Java BitSet toByteArray(BitSet bitSet)

Here you can find the source of toByteArray(BitSet bitSet)

Description

to Byte Array

License

BSD License

Declaration

public static byte[] toByteArray(BitSet bitSet) 

Method Source Code


//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));
    }
}

Related

  1. toBitSet(String s, int length)
  2. toByteArray(BitSet bits)
  3. toByteArray(BitSet bits)
  4. toByteArray(BitSet bits, int fixedNumBytes)
  5. toByteArray(BitSet bits, int sizeInBytes)
  6. toByteArray(BitSet bs, int length)
  7. toFixedLengthByteArray(BitSet bs, int length)
  8. toggleInPlace(BitSet a, BitSet b)
  9. toInt(BitSet bitSet)