Converts a byte array to a long. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Converts a byte array to a long.

Demo Code


//package com.java2s;

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(byteArrayToLong(b));
    }//from  w w w  .j  a v a  2s  . com

    /**
     * Converts a byte array to a long.
     * @param b - The byte array. Must be a length of at least 8.
     * @return The long.
     */
    public static long byteArrayToLong(byte[] b) {
        return b[7] & 0xFF | (b[6] & 0xFF) << 8 | (b[5] & 0xFF) << 16
                | (b[4] & 0xFF) << 24 | (b[3] & 0xFF) << 32L
                | (b[2] & 0xFF) << 40L | (b[1] & 0xFF) << 48L
                | (b[0] & 0xFF) << 56L;
    }
}

Related Tutorials