Java BitSet To Byte Array bitSetToBinaryString(BitSet b, int startBit, int length)

Here you can find the source of bitSetToBinaryString(BitSet b, int startBit, int length)

Description

Convert a BitSet to a binary representation string.

License

Open Source License

Parameter

Parameter Description
b BitSet to convert
startBit Start converting from this bit. Treat it as the least significant bit.
length Number of bits to read from the BitSet after the startBit.

Return

String representation of b.

Declaration

public static String bitSetToBinaryString(BitSet b, int startBit, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.BitSet;

public class Main {
    /**//from ww w .j a  va  2  s  .  c  o m
     * Convert a BitSet to a binary representation string.
     * Each bit is converted into "0" or "1" and added to the string.
     * The result is a binary number with its most significant bit on the left.
     * 
     * @param b BitSet to convert
     * @param startBit Start converting from this bit. Treat it as the least significant bit.
     * @param length Number of bits to read from the BitSet after the startBit.
     * @return String representation of b.
     */
    public static String bitSetToBinaryString(BitSet b, int startBit, int length) {
        return bitSetToBinaryString(b, startBit, length, 4);
    }

    /**
     * Convert a BitSet to a binary representation string.
     * Converting each bit into "0" or "1".
     * The result is a binary number with its most significant bit on the left.
     * 
     * @param b BitSet to convert
     * @param startBit Start converting from this bit. Treat it as the least significant bit.
     * @param length Number of bits to read from the BitSet after the startBit.
     * @param wordlength When greater than 0, a space character is added each wordlength number of digits.
     * @return Binary representation String of the BitSet.
     */
    public static String bitSetToBinaryString(BitSet b, int startBit, int length, int wordlength) {
        StringBuilder ret = new StringBuilder();

        for (int i = length + startBit - 1; i >= startBit; i--) {
            ret.append(b.get(i) ? "1" : "0");

            if (wordlength != -1 && wordlength > 0 && i > 0 && i % wordlength == 0)
                ret.append(' ');
        }

        return ret.toString();
    }
}

Related

  1. bitsetToBinary(BitSet set, int n)
  2. bitSetToBinaryString(BitSet strainBitSet)
  3. bitSetToByteArray(BitSet bitSet, int bitSetLength)
  4. bitSetToByteArray(BitSet bitSet, int byteLen)
  5. bitSetToByteArray(final BitSet bitSet, final int numBytes)