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:
afterProcessing.getBytes();new BigInteger(afterProcessing, 2).toByteArray();- solutions from here: How to convert binary string to the byte array of 2 bytes in java