0

I have the next method which converts a byte array to a binary string:

private static String byteArrayToBinaryString(byte[] arr) {
        StringBuilder sb = new StringBuilder();
        for (byte b : arr) {
            sb.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
        }
        return sb.toString();
    }

It converts this array:

byte[] arr = {(byte) 0xF0, (byte) 0x99, (byte) 0x90, 0x77, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00};

to this string:

// 11110000100110011001000001110111000010000000000000000000000000000000000000000000
String binaryString = byteArrayToBinaryString(arr);

After some processing my string looks like this (same length, modified little bit in the end):

String afterProcessing = "11110000100110011001000001110111000010000000000000000000000000000000000100101100";

I need to convert it back to the array of exactly 10 bytes. How do I do that?

What I've tried:

IKo
  • 3,761
  • 6
  • 26
  • 49
  • Split by 8 characters, then do something like this: https://stackoverflow.com/a/51430897/11808788 – Charles Feb 09 '22 at 12:43

0 Answers0