I have a binary string String A = "1000000110101110" . I want to convert this string to an array of bytes of length 2 in java
I used this link
I tried converting it to bytes in various ways.
I first converted this string to decimal, and then applied the code to be stored in an array of bytes
int aInt = Integer.parseInt(A, 2); byte[] xByte = new byte[2]; xByte[0] = (byte) ((aInt >> 8) & 0XFF); xByte[1] = (byte) (aInt & 0XFF); System.arraycopy(xByte, 0, record, 0, xByte.length);
But get store values ββin byte array are negative
xByte[0] :-127 xByte[1] :-82
Which are incorrect values.
2. I also tried using
byte[] xByte = ByteBuffer.allocate(2).order(ByteOrder.BIG_ENDIAN).putInt(aInt).array();
But it throws an exception on the specified line, for example
java.nio.Buffer.nextPutIndex(Buffer.java:519) at java.nio.HeapByteBuffer.putInt(HeapByteBuffer.java:366) at org.com.app.convert.generateTemplate(convert.java:266)
What do I need to do now to convert a binary string into a 2 byte byte array? Is there a built-in function in java to get an array of bytes
source share