I am using this code:
byte[] bytes = MESSAGE.getBytes();
StringBuilder str = new StringBuilder();
for (byte b : bytes) {
int val = b;
for (int i = 0; i < 8; i++) {
str.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
and I'm trying to modify it to remove leading zerosfrom the binary code of every ASCII character before it gets appended to binary StringBuilder str. But the problem is the number of leading zeros is unknown and I can only remove them from the entire binary block by adding the following after the first block:
String MSG = "";
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) == '0')
MSG = (str.toString()).substring(i+1,str.length());
else
break;
}
Any ideas?
str.append((val & 128) == 0 ? 0 : 1);