title: How to convert bytes to long in java
body: <p>Here i have one String and i converted this string to long and than this long value converted into bytes. This byte array length is 6.</p>
  
  <pre><code>    final byte[] tagBytes = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN)
              .putLong(Long.parseLong("02201156116")).array();
      System.arraycopy(tagBytes, 0, tag, 0, 6);
      Log.d("TAG", "TAG in bytes "+Arrays.toString(tag));
  </code></pre>
  
  <p>Here i got output like this <code>[52, 84, 49, -125, 0, 0]</code></p>
  
  <p>Now i want to convert this output bytes in String as like before <code>"02201156116"</code> </p>
  
  <p>So here i have to first order bytes in BIG_ENDIAN first and then wrap Bytes and get long value from it. 
  But Here i am Surprised why it is not work for </p>
  
  <pre><code>ByteBuffer.wrap(tempDateBytes).order(ByteOrder.BIG_ENDIAN).getLong() 
  </code></pre>
  
  <p>this and its work for </p>
  
  <p><code>ByteBuffer.wrap(tempDateBytes).order(ByteOrder.LITTLE_ENDIAN).getLong()</code> this, By d way we have to Apply BIG_ENDIAN becuase when we convert long to bytes we used LITTLE_ENDIAN. So i think we have to use here BIG_ENDIAN. but its not works for BIG_ENDIAN, its works for LITTLE_ENDIAN.</p>
  
  <p>This code i used to convert <code>[52, 84, 49, -125, 0, 0]</code> to <code>02201156116</code></p>
  
  <pre><code>    long time,time1;
      final byte[] tempDateBytes = new byte[8];
      System.arraycopy(tagBytes, 0, tempDateBytes, 0, 6);
      time1 = ByteBuffer.wrap(tempDateBytes).order(ByteOrder.BIG_ENDIAN).getLong();
      time = ByteBuffer.wrap(tempDateBytes).order(ByteOrder.LITTLE_ENDIAN).getLong();
      Log.d("TAG", "Orignal TAG "+time);
      Log.d("TAG", "BAD TAG  "+time1);
  </code></pre>