get Long from byte array via ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer Long

Description

get Long from byte array via ByteBuffer

Demo Code

/*/* w w  w.j  a  v  a 2 s  .  com*/
 * Copyright (c) 2014 CIYAM Developers

 Distributed under the MIT/X11 software license, please refer to the file license.txt
 in the root project directory or http://www.opensource.org/licenses/mit-license.php.

 */
//package com.java2s;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(getLong(b));
    }

    public static long getLong(byte[] b) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.position(0);
        buffer.put(b);
        return buffer.getLong(0);
    }
}

Related Tutorials