title: Set the Byte values in java
body: <p>I want to convert the binary string to byte array in <code>java</code> . I have write the code to set every bit of the byte array from the binary string <code>String A = "1000000111010000"</code></p>
  
  <pre><code>        private byte firstByte;
      private byte secondByte;
          byte xByte = new byte[2];
  
          for(int i=0 ; i&lt;A.length() ;i++){
  
              if(i&lt;8){
                  System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
                  firstByte = (byte) (firstByte | (A.charAt(i) &lt;&lt; i));
              }else{
                  System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
                  secondByte = (byte) (secondByte | (A.charAt(i) &lt;&lt; i));
              }
          }
          xByte[0] = firstByte;
          xByte[1] = secondByte;
  </code></pre>
  
  <p>To write the above code i have taken the help from <a href="https://stackoverflow.com/questions/4674006/set-specific-bit-in-byte">this</a> link . 
  But the value get stored int the <code>xByte[0]</code> and <code>xByte[1]</code> is not correct.
  It gives values like </p>
  
  <pre><code>                   xByte[0] :-15
                     xByte[1] :0
  </code></pre>
  
  <p>Is this is the write way?Please suggest me the correction to get the right byte values.</p>