Convert binary string to byte array

Parse it to an integer in base 2, then convert to a byte array. In fact, since you’ve got 16 bits it’s time to break out the rarely used short.

short a = Short.parseShort(b, 2);
ByteBuffer bytes = ByteBuffer.allocate(2).putShort(a);

byte[] array = bytes.array();

Leave a Comment