Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert binary string to the byte array of 2 bytes in java

I have binary string String A = "1000000110101110". I want to convert this string into byte array of length 2 in java

I have taken the help of this link

I have tried to convert it into byte by various ways

  1. I have converted that string into decimal first and then apply the code to store into the byte array

    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 the values get store into the byte array are negative

xByte[0] :-127
xByte[1] :-82

Which are wrong values.

2.I have also tried using

byte[] xByte = ByteBuffer.allocate(2).order(ByteOrder.BIG_ENDIAN).putInt(aInt).array();

But it throws the exception at the above line like

  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 should i do now to convert the binary string to byte array of 2 bytes?Is there any inbuilt function in java to get the byte array

like image 528
rachana Avatar asked Sep 24 '13 07:09

rachana


People also ask

Can we convert string to byte array in Java?

How to convert String to byte[] in Java? In Java, we can use str. getBytes(StandardCharsets. UTF_8) to convert a String into a byte[] .

Is binary same as byte array?

Byte arrays mostly contain binary data such as an image. If the byte array that you are trying to convert to String contains binary data, then none of the text encodings (UTF_8 etc.) will work.

What is binary byte array?

ByteArray represents bytes, internally using one byte per value which make them space efficient. Because the data are binary, the performance gain of using them in place of string of bytes, is significant.


4 Answers

The answer you are getting

 xByte[0] :-127
 xByte[1] :-82

is right.

This is called 2's compliment Represantation. 1st bit is used as signed bit.

0 for +ve
1 for -ve

if 1st bit is 0 than it calculates as regular. but if 1st bit is 1 than it deduct the values of 7 bit from 128 and what ever the answer is presented in -ve form.

In your case 1st value is10000001 so 1(1st bit) for -ve and 128 - 1(last seven bits) = 127 so value is -127

For more detail read 2's complement representation.

like image 94
Malav Avatar answered Sep 18 '22 15:09

Malav


Use putShort for putting a two byte value. int has four bytes.

// big endian is the default order
byte[] xByte = ByteBuffer.allocate(2).putShort((short)aInt).array();

By the way, your first attempt is perfect. You can’t change the negative sign of the bytes as the most significant bit of these bytes is set. That’s always interpreted as negative value.

10000001₂ == -127

10101110₂ == -82

like image 37
Holger Avatar answered Sep 21 '22 15:09

Holger


try this

String s = "1000000110101110";
int i = Integer.parseInt(s, 2);
byte[] a = {(byte) ( i >> 8), (byte) i};
System.out.println(Arrays.toString(a));
System.out.print(Integer.toBinaryString(0xFF & a[0]) + " " + Integer.toBinaryString(0xFF & a[1]));

output

[-127, -82]
10000001 10101110

that is -127 == 0xb10000001 and -82 == 0xb10101110

like image 20
Evgeniy Dorofeev Avatar answered Sep 21 '22 15:09

Evgeniy Dorofeev


Bytes are signed 8 bit integers. As such your result is completely correct. That is: 01111111 is 127, but 10000000 is -128. If you want to get numbers in 0-255 range you need to use a bigger variable type like short.

You can print byte as unsigned like this:

public static String toString(byte b) {
    return String.valueOf(((short)b) & 0xFF);
}
like image 41
RokL Avatar answered Sep 19 '22 15:09

RokL