Java Binary Encode toBinFromByte(final byte b)

Here you can find the source of toBinFromByte(final byte b)

Description

Transform a byte into a bitstring (of length 8)

License

Open Source License

Parameter

Parameter Description
b The byte to convert

Return

The binary String representing the input byte

Declaration

public static String toBinFromByte(final byte b) 

Method Source Code

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

public class Main {
    /** Allowable binary values */
    private final static String[] binSymbols = { "0", "1" };

    /**/*from   www.  j av a2  s.  c o m*/
     * Transform a byte into a bitstring (of length 8)
     *
     * @param b
     *            The byte to convert
     * @return The binary String representing the input byte
     */
    public static String toBinFromByte(final byte b) {
        StringBuilder binBuffer = new StringBuilder(8);

        // We need to read each of the 8 bits out of the
        // input byte and append them one by one to the
        // output bit string
        binBuffer.append(binSymbols[((b & 0x80) >>> 7)]);
        binBuffer.append(binSymbols[((b & 0x40) >>> 6)]);
        binBuffer.append(binSymbols[((b & 0x20) >>> 5)]);
        binBuffer.append(binSymbols[((b & 0x10) >>> 4)]);
        binBuffer.append(binSymbols[((b & 0x08) >>> 3)]);
        binBuffer.append(binSymbols[((b & 0x04) >>> 2)]);
        binBuffer.append(binSymbols[((b & 0x02) >>> 1)]);
        binBuffer.append(binSymbols[(b & 0x01)]);

        return (binBuffer.toString());
    }
}

Related

  1. toBinaryString(int[] input)
  2. toBinaryString(long val)
  3. toBinaryString(long value, int bitLegnth)
  4. toBinaryStringAddress(long address)
  5. toBinaryText(StringBuffer buf)
  6. toBinFromHex(final String hexSymbols)
  7. toBinFromHexChar(final char hex)
  8. toBinFromOct(final String octSymbols)
  9. toBinFromOctChar(final char oct)