Java Binary Encode toBinString(byte b)

Here you can find the source of toBinString(byte b)

Description

An byte as a string bit representation.

License

Mozilla Public License

Return

b as binary String (e.g. "10110111").

Declaration

public static String toBinString(byte b) 

Method Source Code

//package com.java2s;
//License from project: Mozilla Public License 

public class Main {
    /** An byte as a string bit representation.
     * @return <code>b</code> as binary String (e.g. "10110111"). */
    public static String toBinString(byte b) {
        return toBinString((int) b);
    }//from   ww  w .j  a  v a2  s .  c  o m

    /** An int as a string bit representation.
     * @return <code>i</code> as binary String (e.g. "10110111"). */
    public static String toBinString(int i) {
        boolean bT = ((i & 128) == 128);
        String s = (bT ? "1" : "0");
        bT = ((i & 64) == 64);
        s += (bT ? "1" : "0");
        bT = ((i & 32) == 32);
        s += (bT ? "1" : "0");
        bT = ((i & 16) == 16);
        s += (bT ? "1" : "0");
        bT = ((i & 8) == 8);
        s += (bT ? "1" : "0");
        bT = ((i & 4) == 4);
        s += (bT ? "1" : "0");
        bT = ((i & 2) == 2);
        s += (bT ? "1" : "0");
        bT = ((i & 1) == 1);
        s += (bT ? "1" : "0");
        return s;
    }
}

Related

  1. toBinFromByte(final byte b)
  2. toBinFromHex(final String hexSymbols)
  3. toBinFromHexChar(final char hex)
  4. toBinFromOct(final String octSymbols)
  5. toBinFromOctChar(final char oct)
  6. toBinString(byte value)
  7. toBinString(byte[] byteArray)
  8. toBinString(int val, int minLength)