title: Working around no unsigned variables in java
body: <p>I'm writing file save and load functions for a specific file format that I have no control over and the format specifies that at a specific byte position I must write 4 bytes of data to signify a 32bit unsigned value... in my test file this value is 16052, or 0x00003EB4... so I write to the data to the byte array that will be saved in this manner:</p>
  
  <pre><code>data[index] = 0xB4;
  data[index+1] = 0x3E;
  data[index+2] = 0x00;
  data[index+3] = 0x00;
  </code></pre>
  
  <p>You can see the data is in little-endian format, which is correct... the problem is when I try to load this data with my file load function java sees the data in as such:</p>
  
  <p>-76, 62, 0, 0</p>
  
  <p>the 0xB4 value is being interpreted as -76 because bytes are signed in java... when I try to recompose these 4 bytes into a single 32bit value using the following code the value ends up as -76...</p>
  
  <pre><code>value = data[index+3];
  value &lt;&lt;= 8;
  value |= data[index+2];
  value &lt;&lt;= 8;
  value |= data[index+1];
  value &lt;&lt;= 8;
  value |= data[index];
  </code></pre>
  
  <p>What this should do is the following:
  set value to 0x00 (high order byte), shift left 8 bits, or 0x00 onto the lower 8 bits, shift left 8 bits, or 0x3E onto the lower 8 bits, shift left 8 bits, or 0xB4 (low order byte) onto the lower 8 bits.</p>
  
  <p>This should produce the value 0x00003EB4... which is what I started with... however for some reason I cannot figure out it is giving me the value -76 after that operation.</p>
  
  <p>I am convinced this is due to java's interpretation of the 0xB4 byte as the value -76 which is screwing up the bitwise OR operation...</p>
  
  <p>My question is what must I do to get around this?</p>
  
  <p>Thank you.</p>