A simple method that turns a byte array in to a String containing the binary value.
String bytesToBinaryString(byte[] bytes){
StringBuilder binaryString = new StringBuilder();
/* Iterate each byte in the byte array */
for(byte b : bytes){
/* Initialize mask to 1000000 */
int mask = 0x80;
/* Iterate over current byte, bit by bit.*/
for(int i = 0; i < 8; i++){
/* Apply mask to current byte with AND,
* if result is 0 then current bit is 0. Otherwise 1.*/
binaryString.append((mask & b) == 0 ? "0" : "1");
/* bit-wise right shift the mask 1 position. */
mask >>>= 1;
}
}
/* Return the resulting 'binary' String.*/
return binaryString.toString();
}