Here you can find the source of toBinaryString(char value)
public static String toBinaryString(char value)
//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); } }