rachana
rachana

Reputation: 3414

Set the Byte values in java

I want to convert the binary string to byte array in java . I have write the code to set every bit of the byte array from the binary string String A = "1000000111010000"

        private byte firstByte;
    private byte secondByte;
        byte xByte = new byte[2];

        for(int i=0 ; i<A.length() ;i++){

            if(i<8){
                System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
                firstByte = (byte) (firstByte | (A.charAt(i) << i));
            }else{
                System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
                secondByte = (byte) (secondByte | (A.charAt(i) << i));
            }
        }
        xByte[0] = firstByte;
        xByte[1] = secondByte;

To write the above code i have taken the help from this link . But the value get stored int the xByte[0] and xByte[1] is not correct. It gives values like

                   xByte[0] :-15
                   xByte[1] :0

Is this is the write way?Please suggest me the correction to get the right byte values.

Upvotes: 2

Views: 29241

Answers (3)

Sergej Panic
Sergej Panic

Reputation: 839

Just use BinaryCodec from Apache Commons:

 byte[] bytes = new BinaryCodec().toByteArray("1000000111010000");

   

If you want to do such conversion on your own, your code needs some corrections.
You are expecting that A.charAt(i) will return numeric 0 or 1, but will actually return char '0' or '1'. The char data type is a single 16-bit Unicode character with a numeric range from 0 to 2^16, it's values are formally called code points.
To print the code point value you need to cast char to int:

System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

Output for '0' and '1':

Character 0 has a code point numeric value of 48
Character 1 has a code point numeric value of 49

 

Operator '<<' converts char operands to int, therefore this shifting is producing wrong results because:

firstByte = (byte) (firstByte | (A.charAt(i) << i));

is the same as

firstByte = (byte) (firstByte | ( (int)A.charAt(i) << i));

which for char '0' is the same as shifting value 48 to the left:

firstByte = (byte) (firstByte | ( 48 << i));

 

To convert char '0' or '1' to 0 or 1 numeric value use Character.getNumericValue(A.charAt(i)):

firstByte = (byte) (firstByte | ( Character.getNumericValue(A.charAt(i)) << i));

   

Also shifting by value i is incorrect. You need to shift by (7-i) for the first byte or (7-i%8) for the second byte. When index i reaches 8 it needs to start counting from 0, therefore i%8

   

When printing a values for a byte type you have two options: byte numeric value or binary string representation:

System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));

output:

FIRST  byte value = -127, binary representation = 10000001
SECOND byte value = -48, binary representation = 11010000

Whole corrected example:

public class ByteTest
{

  public static void main(String[] args)
  {

    byte firstByte=0;
    byte secondByte=0;

    String A = "1000000111010000";
    byte[] xByte = new byte[2];





    for(int i=0 ; i<A.length() ;i++){

      System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

      if(i<8){
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        firstByte = (byte) (firstByte | (Character.getNumericValue(A.charAt(i)) << (7-i)));
      }else{
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        secondByte = (byte) (secondByte | (Character.getNumericValue(A.charAt(i)) << (7-i%8)));
      }
    }
    xByte[0] = firstByte;
    xByte[1] = secondByte;


    System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
    System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));




  }

}

Upvotes: 5

Rahul
Rahul

Reputation: 45060

You can't simply cast the A.charAt(i) to an int. It'll return the ASCII code of the 1 and 0.

Therefore, you need to do something like this to get their numeric value:-

int bit = Character.getNumericValue(A.charAt(i)); // This will give the actual value
...
firstByte = (byte) (firstByte | (bit << i));

Upvotes: 4

code_fish
code_fish

Reputation: 3436

A.charAt[i] will return a char, not a number and you are setting bit to a char value.

Instead use

if(A.charAt[i] == '0')
  firstByte = (byte) (firstByte | (0 << i));
else
   firstByte = (byte) (firstByte | (1 << i));

Upvotes: 1

Related Questions