Here you can find the source of toLong(byte[] bytes)
Parameter | Description |
---|---|
bytes | array |
public static long toLong(byte[] bytes)
//package com.java2s; //License from project: Apache License public class Main { /**// ww w.j a va 2s . com * Size of long in bytes */ public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE; /** * Converts a byte array to a long value. Reverses * {@link #toBytes(long)} * * @param bytes array * @return the long value */ public static long toLong(byte[] bytes) { return toLong(bytes, 0, SIZEOF_LONG); } /** * Converts a byte array to a long value. Assumes there will be * {@link #SIZEOF_LONG} bytes available. * * @param bytes bytes * @param offset offset * @return the long value */ public static long toLong(byte[] bytes, int offset) { return toLong(bytes, offset, SIZEOF_LONG); } /** * Converts a byte array to a long value. * * @param bytes array of bytes * @param offset offset into array * @param length length of data (must be {@link #SIZEOF_LONG}) * @return the long value * @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or * if there's not enough room in the array at the offset indicated. */ public static long toLong(byte[] bytes, int offset, final int length) { if (length != SIZEOF_LONG || offset + length > bytes.length) { throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG); } long l = 0; for (int i = offset; i < offset + length; i++) { l <<= 8; l ^= bytes[i] & 0xFF; } return l; } private static IllegalArgumentException explainWrongLengthOrOffset(final byte[] bytes, final int offset, final int length, final int expectedLength) { String reason; if (length != expectedLength) { reason = "Wrong length: " + length + ", expected " + expectedLength; } else { reason = "offset (" + offset + ") + length (" + length + ") exceed the" + " capacity of the array: " + bytes.length; } return new IllegalArgumentException(reason); } }