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

4

6

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

Oberhausen answered 24/9, 2013 at 7:45 Comment(0)
A
2

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.

Albumenize answered 24/9, 2013 at 8:39 Comment(0)
I
2

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

Idoux answered 24/9, 2013 at 7:48 Comment(3)
That’s more compact but does not differ from what the questioner already did in the first attempt.Reft
I have tried with your method byte[] xByte = {(byte) ( aInt >> 8), (byte) aInt}; But the value stored in the zeroth place is still negative xByte[0] :-127Oberhausen
@rachana: And that won’t change. See my answer.Reft
R
2

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

Reft answered 24/9, 2013 at 7:49 Comment(1)
Can you please explain with an example?Oberhausen
H
2

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);
}
Housel answered 24/9, 2013 at 8:1 Comment(4)
can you please explain that with an example?Oberhausen
Explain what? How to use shorts? Just replace byte[] with short[] and replace (byte) casts with (short) casts and you'll get 0-255 values. Java byte represents numbers in [-128, 127] range.Housel
But I want the byte array if I use short[] replacing byte[] it will give short[] and this is not what I wantOberhausen
well with byte array each element represents 256 possible values, which are in -128 to 127 range as far as arithmetic operations are concerned. Also java built-in functions to print them to String are going to use these values. You can print bytes to 0-255 strings yourself if you want to. I will edit my answer with a function that prints a byte in that range.Housel
A
2

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.

Albumenize answered 24/9, 2013 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.