title: How do I convert a byte array to a long in Java?
body: <p>I am reading 8 bytes of data in from a hardware device.  I need to convert them into a numeric value.  I think I want to convert them to a long as that should fit 8 bytes.  I am not very familiar with Java and low level data type operations. I seem to have two problems (apart from the fact there is almost no documentation for the hardware in question), The bytes are expecting to be unsigned, so I can't do a straight integer conversion.  I am not sure what endianness they are.</p>
  
  <p>Any advice would be appreciated.</p>
  
  <hr>
  
  <p>Ended up with this (taken from some source code I probably should have read a week ago):</p>
  
  <pre><code>public static final long toLong (byte[] byteArray, int offset, int len)
  {
     long val = 0;
     len = Math.min(len, 8);
     for (int i = (len - 1); i &gt;= 0; i--)
     {
        val &lt;&lt;= 8;
        val |= (byteArray [offset + i] &amp; 0x00FF);
     }
     return val;
  }
  </code></pre>