I have a binary string and I want to split it into chunks of length 8 and then store the corresponding bytes in a byte-Array. For example, the string "0000000011111111" should be convertert to {-128, 127}. So far, I wrote the following function:
public static byte[] splitBinary(String binaryString) throws ConversionException {
if (binaryString.length()%8 != 0) {
throw new ConversionException();
}
byte[] result = new byte[binaryString.length()/8];
while (i < result.length) {
result[i] = (byte) (Integer.parseInt(binaryString.substring(i, i+8), 2)-128);
i+=8;
}
return result;
}
But this results in {-128, 0}. How can I achieve the desired functionality?
result[8]which is invalid.