tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Basics > How to convert Byte Array to Long

How to convert Byte Array to Long 

The following example shows converting a Byte array to Long. First create a ByteBuffer with given byte array and then read next 8 bytes from it as a Long.

java.nio.ByteBuffer
  • public static ByteBuffer wrap(byte[] array)
    Wraps a byte array into a buffer.
  • public long getLong()
    Reads the next eight bytes in the ByteBuffer as a Long.

File Name  :  
com/bethecoder/articles/basics/prim2bytea/ByteArray2LongTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.articles.basics.prim2bytea;

import java.nio.ByteBuffer;

public class ByteArray2LongTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //byte [] bytes = ByteBuffer.allocate(8).putLong(1729172917291729L).array();
    byte [] bytes = 0636, -84113125, -118, -47 };
    System.out.println(ByteBuffer.wrap(bytes).getLong());
  }

}
   

It gives the following output,
1729172917291729



 
  


  
bl  br