title: How to convert negative byte value to either short or integer?
body: <p>We have a file which contains byte array in particular format like header and then followed by data. This file is generated by another java program and then we are reading that same file through another java program in the same format in which it was written.</p>
  
  <p>The program which is making that file, populates <code>lengthOfKey</code> as byte which is right as that's what we need as shown below.</p>
  
  <pre><code>    for (Map.Entry&lt;byte[], byte[]&gt; entry : holder.entrySet()) {
          byte typeKey = 0;
  
          // getting the key length as byte (that's what we need to do)
          byte lengthOfKey = (byte) entry.getKey().length;
          byte[] actualKey = entry.getKey();
      }
  </code></pre>
  
  <p>Now as <code>byte</code> can only store maximum value as <code>127</code>, we are seeing for some of our record <code>lengthOfKey</code> is coming as negative while we read the file as shown below:</p>
  
  <p>Program which is reading the file:</p>
  
  <pre><code>byte keyType = dis.readByte();
  
  // for some record this is coming as negative. For example -74
  byte lengthOfKey = dis.readByte();
  </code></pre>
  
  <p><strong>Now my question is :</strong> Is there any way I can find out what was the actual length of key because of that it got converted to <code>-74</code> while writing it. I mean it should be greater than <code>127</code> and that's why it got converted to <code>-74</code> but what was the actual value?</p>
  
  <p>I think question would be how to convert negative byte value to either short or integer? I just wanted to verify to see what was the actual length of key because of that it got converted to negative value.</p>