It will give one implementation on how to convert byte to bit string (8 bits). For example: byte 5 will change to “00000101”, byte 8 will be “00001000”.
1 2 3 4 5 6 7 8 9 10 11 |
public String toBits(final byte val) { final StringBuilder result = new StringBuilder(); for (int i=0; i<8; i++) { result.append((int)(val >> (8-(i+1)) & 0x0001)); } return result.toString(); } |
In addition, if you are using Java implementation, you can consider
1 2 3 4 5 6 |
public String toBitString(final byte val) { return String.format("%8s", Integer.toBinaryString(val & 0xFF)) .replace(' ', '0'); } |
Hi admin, your page is fantastic.