3

I have a large binary string "101101110...", and I am trying to store it into a byte array. what is the best way of doing it?

Lets say I have largeString = "0100111010111011011000000001000110101"

Result that I'm looking for:

[78, 187, 96, 17, 21]

01001110 10111011 01100000 00010001 10101

What i've tried:

byte[] b= new BigInteger(largeString,2).toByteArray();

however it did not give me the result I'm looking for...

4
  • So you want the bytes left-aligned? Also, what output did you get vs the output you were expecting? Commented May 14, 2014 at 20:10
  • yes, i wish it to be left aligned. the output i got with the attempt is: [9, -41, 108, 2, 53] 1001 11010111 1101100 10 110101 Commented May 14, 2014 at 20:12
  • A byte cannot hold 187. The range value is [-128, 127] Commented May 14, 2014 at 20:13
  • @ZouZou It technically can in Java; it just won't display as 187 Commented May 14, 2014 at 20:13

3 Answers 3

7

You can easily build an ArrayList on which you can call toArray if you want an actual array;

List<Integer> list = new ArrayList<>();

for(String str : largeString.split("(?<=\\G.{8})"))
    list.add(Integer.parseInt(str, 2));

System.out.println(list);  // Outputs [78, 187, 96, 17, 21]
Sign up to request clarification or add additional context in comments.

1 Comment

okay, i shame with my hardcoded substring example. VERY NICE use of regex ! Nice solution!!
1

Assuming that your binary string module 8 equals 0 binString.lenght()%8==0

 /**
 * Get an byte array by binary string
 * @param binaryString the string representing a byte
 * @return an byte array
 */
public static byte[] getByteByString(String binaryString) {
    int splitSize = 8;

    if(binaryString.length() % splitSize == 0){
        int index = 0;
        int position = 0;

        byte[] resultByteArray = new byte[binaryString.length()/splitSize];
        StringBuilder text = new StringBuilder(binaryString);

        while (index < text.length()) {
            String binaryStringChunk = text.substring(index, Math.min(index + splitSize, text.length()));
            Integer byteAsInt = Integer.parseInt(binaryStringChunk, 2);
            resultByteArray[position] = byteAsInt.byteValue();
            index += splitSize;
            position ++;
        }
        return resultByteArray;
    }
    else{
        System.out.println("Cannot convert binary string to byte[], because of the input length. '" +binaryString+"' % 8 != 0");
        return null;
    }
}

Comments

0

Do it in a loop. Split the string at 8-character chunks and convert them separately. In "pseudocode" it's something like:

byte[] result = new byte[subs.size()];

int i = 0;
int j = 0;
while(i+8 <= s.length){
    result[j] = new Byte.valueOf(largeString.substring(i, i+8), 2);
    i+=8;
    j++;
}

result[j] = new Byte.valueOf(largeString.substring(i, largeString.length));

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.