title: how to read signed int from bytes in java?
body: <p>I have a spec which reads the next two bytes are signed int.</p>
  
  <p>To read that in java i have the following</p>
  
  <p>When i read a signed int in java using the following code i get a value of 65449</p>
  
  <p>Logic for calculation of unsigned</p>
  
  <pre><code>int a =(byte[1] &amp; 0xff) &lt;&lt;8
  
  int b =(byte[0] &amp; 0xff) &lt;&lt;0
  
  int c = a+b 
  </code></pre>
  
  <p>I believe this is wrong because if i and with 0xff i get an unsigned equivalent</p>
  
  <p>so i removed the &amp; 0xff and the logic as given below</p>
  
  <pre><code>int a = byte[1] &lt;&lt;8
  int b = byte[0] &lt;&lt; 0
  int c = a+b
  which gives me the value -343
  byte[1] =-1
  byte[0]=-87
  </code></pre>
  
  <p>I tried to offset these values with the way the spec reads but this looks wrong.Since the size of the heap doesnt fall under this.</p>
  
  <p>Which is the right way to do for signed int calculation in java?</p>
  
  <p>Here is how the spec goes</p>
  
  <p><code>somespec() { xtype 8 uint8 xStyle 16 int16 }</code> 
   xStyle :A signed integer that represents an offset (in bytes) from the start of this Widget() structure to the start of an xStyle() structure that expresses inherited styles for defined by page widget as well as styles that apply specifically to this widget.</p>