Java Long Number Create toLong(final byte[] data)

Here you can find the source of toLong(final byte[] data)

Description

Converts the 8-byte value to a Big Endian long

License

Open Source License

Parameter

Parameter Description
data byte array to convert

Return

Long of the given byte array

Declaration

public static long toLong(final byte[] data) 

Method Source Code

//package com.java2s;
/*//from   www.  ja va  2  s  . c o m
 * File:                HashFunctionUtil.java
 * Authors:             Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright Jan 26, 2011, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government.
 * Export of this program may require a license from the United States
 * Government. See CopyrightHistory.txt for complete details.
 *
 */

public class Main {
    /**
     * Converts the 8-byte value to a Big Endian long
     * @param data
     * byte array to convert
     * @return
     * Long of the given byte array
     */
    public static long toLong(final byte[] data) {
        if (data.length != 8) {
            throw new IllegalArgumentException("data must be of length 8");
        }
        return toLong(data, 0);
    }

    /**
     * Converts the 8-byte value at "offset" to a Big Endian long
     * @param data
     * byte array to convert
     * @param offset
     * Offset into the byte array
     * @return
     * Long at the given offset
     */
    public static long toLong(final byte[] data, final int offset) {
        return ((data[offset] & 0xffL) << 56) | ((data[offset + 1] & 0xffL) << 48)
                | ((data[offset + 2] & 0xffL) << 40) | ((data[offset + 3] & 0xffL) << 32)
                | ((data[offset + 4] & 0xffL) << 24) | ((data[offset + 5] & 0xffL) << 16)
                | ((data[offset + 6] & 0xffL) << 8) | ((data[offset + 7] & 0xffL));
    }
}

Related

  1. toLong(final byte[] array, final int offset, final int length)
  2. toLong(final byte[] b)
  3. toLong(final byte[] b, final int offset)
  4. toLong(final byte[] bytes, final int offset)
  5. toLong(final byte[] data)
  6. toLong(final byte[] inputBytes, int offset)
  7. toLong(final Object o, final String pattern)
  8. toLong(final Object obj)
  9. toLong(final String bitString)