Convert byte to binary - Java java.lang

Java examples for java.lang:int Binary

Description

Convert byte to binary

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        byte inputByte = 42;
        System.out.println(byte2bin(inputByte));
    }/* w  w w .j av  a  2s .  c o m*/

    /**
     * Convert byte to binary
     * @param inputByte
     * @return
     */
    public static String byte2bin(byte inputByte) {
        char lbinDigit[] = { '0', '1' };
        char[] lArray = { lbinDigit[(inputByte >> 7) & 0x01],

        lbinDigit[(inputByte >> 6) & 0x01],
                lbinDigit[(inputByte >> 5) & 0x01],
                lbinDigit[(inputByte >> 4) & 0x01],
                lbinDigit[(inputByte >> 3) & 0x01],
                lbinDigit[(inputByte >> 2) & 0x01],
                lbinDigit[(inputByte >> 1) & 0x01],
                lbinDigit[inputByte & 0x01] };

        return new String(lArray);
    }
}

Related Tutorials