Java Binary Encode toBinaryString(char value)

Here you can find the source of toBinaryString(char value)

Description

to Binary String

License

Open Source License

Declaration

public static String toBinaryString(char value) 

Method Source Code

//package com.java2s;

public class Main {
    static final int[] mask = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 };
    static byte b0 = (byte) '0';
    static byte b1 = (byte) '1';

    public static String toBinaryString(byte value) {
        byte[] b = new byte[8];
        int cnt = 0;
        for (int i = 7; i > -1; i--) {
            b[cnt++] = (value & mask[i]) == 0 ? b0 : b1;
        }//from  ww  w.j ava2  s . co m
        return new String(b);
    }

    public static String toBinaryString(char value) {
        byte[] b = new byte[16];
        int cnt = 0;
        for (int i = 15; i > -1; i--) {
            b[cnt++] = (value & mask[i]) == 0 ? b0 : b1;
        }
        return new String(b);
    }

    public static String toBinaryString(int value, int length) {
        byte[] b = new byte[length];
        int cnt = 0;
        for (int i = length - 1; i > -1; i--) {
            if (((value >> i) & 1) == 1) {
                b[cnt++] = b1;
            } else {
                b[cnt++] = b0;
            }
        }
        return new String(b);
    }
}

Related

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