List of utility methods to do Byte to Bit
String | byteToBit(byte b) byte To Bit return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1) + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1); |
boolean[] | byteToBitArray(byte b) byte To Bit Array boolean[] buff = new boolean[8]; int index = 0; for (int i = 7; i >= 0; i--) { buff[index++] = ((b >>> i) & 1) == 1; return buff; |
String | byteToBits(byte b) byte To Bits String bits = Integer.toBinaryString(b & 0xFF); while (bits.length() < 8) { bits = "0" + bits; return bits; |
String | byteToBits(byte b) byte To Bits StringBuffer buf = new StringBuffer(); int n = 256; for (int i = 0; i < 8; i++) { n /= 2; if ((b & n) != 0) buf.append('1'); else buf.append('0'); ... |
boolean[] | byteToBits(byte b) byte To Bits boolean[] bits = new boolean[8]; for (int i = 0; i < 8; i++) { bits[7 - i] = ((b & (1 << i)) != 0); return bits; |
String | byteToBits(byte b) byte To Bits StringBuffer buf = new StringBuffer(); for (int i = 0; i < 8; i++) buf.append((int) (b >> (8 - (i + 1)) & 0x0001)); return buf.toString(); |
String | byteToBits(byte b) byte To Bits return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1) + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1); |
byte[] | byteToBitsArray(byte b) This method convert a single byte to a 8-bytes array. byte[] result = new byte[8]; short val = (short) (b + 128); for (int i = 0; i < 8; i++) { result[7 - i] = (byte) (val % 2); val /= 2; return result; |
String | byteToBitStr(byte b) byte To Bit Str String zero = "00000000"; String s = Integer.toBinaryString(b); return zero.substring(0, 8 - s.length()) + s; |