Java Byte Array to Long bytesToLong(byte[] bytes, int off)

Here you can find the source of bytesToLong(byte[] bytes, int off)

Description

Constructs long from byte array.

License

GNU General Public License

Parameter

Parameter Description
bytes Array of bytes.
off Offset in bytes array.

Return

Long value.

Declaration

public static long bytesToLong(byte[] bytes, int off) 

Method Source Code

//package com.java2s;
// Copyright (C) GridGain Systems Licensed under GPLv3, http://www.gnu.org/licenses/gpl.html

public class Main {
    /**//from  www.j a  va2  s . c  om
     * Constructs {@code long} from byte array.
     *
     * @param bytes Array of bytes.
     * @param off Offset in {@code bytes} array.
     * @return Long value.
     */
    public static long bytesToLong(byte[] bytes, int off) {
        return fromBytes(bytes, off, 8);
    }

    /**
     * Constructs {@code long} from byte array. The first byte in array is the highest byte in result.
     *
     * @param bytes Array of bytes.
     * @param offset Offset in {@code bytes} array.
     * @param limit Amount of bytes to use in the source array.
     * @return Unsigned long value.
     */
    private static long fromBytes(byte[] bytes, int offset, int limit) {
        assert bytes != null;
        assert limit <= 8;
        assert bytes.length >= offset + limit;

        long res = 0;

        for (int i = 0; i < limit; i++) {
            res <<= 8;
            res |= bytes[offset + i] & 0xFF;
        }

        return res;
    }
}

Related

  1. bytesToLong(byte[] bytes)
  2. bytesToLong(byte[] bytes)
  3. bytesToLong(byte[] bytes)
  4. bytesToLong(byte[] bytes)
  5. bytesToLong(byte[] bytes, int index)
  6. bytesToLong(byte[] bytes, int off, boolean bigEndian)
  7. bytesToLong(byte[] data)
  8. bytesToLong(byte[] data)
  9. bytesToLong(byte[] data)