Java Binary Encode toBinary(byte b)

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

Description

to Binary

License

Open Source License

Declaration

public static String toBinary(byte b) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

public class Main {
    public static final String BIN_PREFIX = "0b";

    public static String toBinary(byte b) {
        return toBinary(b, BIN_PREFIX);
    }/*w  w w.jav a  2 s .c o  m*/

    public static String toBinary(byte b, String prefix) {
        String result = "";
        for (int i = 0; i < 8; i++) {
            result += (b >>> (7 - i)) & (0x01);
        }
        return result;
    }

    public static String toBinary(short s, String prefix) {
        String result = toBinary((byte) (s >>> 8), "");
        result += toBinary((byte) (s), "");
        return prefix + result;
    }
}

Related

  1. toBin(int bin, int size)
  2. toBin(int size, String bitString)
  3. toBin(int x)
  4. toBin(long value, int width)
  5. toBinArray(String hexStr)
  6. toBinary(byte b)
  7. toBinary(byte b)
  8. toBinary(byte b)
  9. toBinary(byte[] bytes)