Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One could use the <a href="http://java.sun.com/javase/6/docs/api/java/nio/Buffer.html" rel="noreferrer"><code>Buffer</code></a>s that are provided as part of the <a href="http://java.sun.com/javase/6/docs/api/java/nio/package-summary.html" rel="noreferrer"><code>java.nio</code></a> package to perform the conversion.</p> <p>Here, the source <code>byte[]</code> array has a of length 8, which is the size that corresponds with a <code>long</code> value.</p> <p>First, the <code>byte[]</code> array is wrapped in a <a href="http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html" rel="noreferrer"><code>ByteBuffer</code></a>, and then the <a href="http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html#getLong()" rel="noreferrer"><code>ByteBuffer.getLong</code></a> method is called to obtain the <code>long</code> value:</p> <pre><code>ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 4}); long l = bb.getLong(); System.out.println(l); </code></pre> <p>Result</p> <pre><code>4 </code></pre> <p>I'd like to thank dfa for pointing out the <code>ByteBuffer.getLong</code> method in the comments.</p> <hr> <p>Although it may not be applicable in this situation, the beauty of the <code>Buffer</code>s come with looking at an array with multiple values.</p> <p>For example, if we had a 8 byte array, and we wanted to view it as two <code>int</code> values, we could wrap the <code>byte[]</code> array in an <code>ByteBuffer</code>, which is viewed as a <a href="http://java.sun.com/javase/6/docs/api/java/nio/IntBuffer.html" rel="noreferrer"><code>IntBuffer</code></a> and obtain the values by <a href="http://java.sun.com/javase/6/docs/api/java/nio/IntBuffer.html#get(int)" rel="noreferrer"><code>IntBuffer.get</code></a>:</p> <pre><code>ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 1, 0, 0, 0, 4}); IntBuffer ib = bb.asIntBuffer(); int i0 = ib.get(0); int i1 = ib.get(1); System.out.println(i0); System.out.println(i1); </code></pre> <p>Result:</p> <pre><code>1 4 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload