Java Binary Encode toBinaryString(final boolean[] bits)

Here you can find the source of toBinaryString(final boolean[] bits)

Description

Converts an array of boolean into a string using the toDigit method.

License

Open Source License

Parameter

Parameter Description
bits Array of booleans to interpret

Return

String, containing binary digits that represent the array

Declaration

public static String toBinaryString(final boolean[] bits) 

Method Source Code

//package com.java2s;

public class Main {
    private static final int TRUE = 1;
    private static final int FALSE = 0;

    /**//  www .ja va2s  .co  m
     * Converts an array of boolean into a string using the toDigit method.
     * 
     * @param bits
     *            Array of booleans to interpret
     * @return String, containing binary digits that represent the array
     */
    public static String toBinaryString(final boolean[] bits) {
        final StringBuilder builder = new StringBuilder(bits.length);
        for (final boolean bit : bits) {
            builder.append(toDigit(bit));
        }
        return builder.toString();
    }

    /**
     * Converts a boolean into a digit.
     * 
     * @param bit
     *            the boolean to interpret
     * @return 1, if bit was <code>true</code>, 0 otherwise
     */
    public static int toDigit(final boolean bit) {
        return (bit) ? TRUE : FALSE;
    }
}

Related

  1. toBinaryString(byte[] bytes)
  2. toBinaryString(byte[] bytes)
  3. toBinaryString(byte[] bytes, int bitSize)
  4. toBinaryString(byte[] data, boolean format)
  5. toBinaryString(char value)
  6. toBinaryString(final byte number)
  7. toBinaryString(final char[] arr)
  8. toBinaryString(int a, int bits)
  9. toBinaryString(int b, int bits)