0

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?

8
  • @SandeepKumar OP wrote should be convertert to {-128, 127} Commented Dec 16, 2019 at 8:28
  • i+=8 in for loop Commented Dec 16, 2019 at 8:29
  • Yes: the input is the string "0000000011111111" and the output shoud be the byte array {-128, 127}. Commented Dec 16, 2019 at 8:29
  • @Vihar No! That would result in accesing result[8] which is invalid. Commented Dec 16, 2019 at 8:31
  • @Seelenvirtuose a different index can be used for result, but according to the question this is the index the next iteration should take iteration 1 : 0 to 7 iteration 2 : 8 to 15 and so on Commented Dec 16, 2019 at 8:33

3 Answers 3

1

I changed the function in the following way and now it works as expected (after correcting my expectations ;)). Thank you all!

public static byte[] splitBinaryStringToByteArray(String binaryString) throws ConversionException {

    if (binaryString.length()%8 != 0) {
        throw new ConversionException();
    }

    byte[] result = new byte[binaryString.length()/8];

    int i =0;int j=0;
    while (i < binaryString.length()) {
        System.out.println("Substring: " + binaryString.substring(i, i+8));
        result[j] =((byte)Integer.parseInt((binaryString.substring(i, i+8)), 2));
        j++;
        i+=8;
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your splitting routine is wrong.

increasing i by 1 just moves your window one charachter to the right, not two, resulting in die Strings 00000000 and 00000001 to be read.

3 Comments

Thank you! I changed it in the question. But now it leads to {-128,0}.
You change your question and the downvote an answer for no longer fitting your changed question?
I did not downvote anyone! I just wanted to use your helpful comment...
0

I refer to result's size, it may smaller than size of binaryString, but in binaryString.substring(i, i+8) and i+=8; you use as size of binaryString. So change them to binaryString.substring(i*8, i*8+8) and i++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.