Java Utililty Methods Byte Array to Long

List of utility methods to do Byte Array to Long

Description

The list of methods to do Byte Array to Long are organized into topic(s).

Method

longbytes2Long(byte[] bytes, int offset)
bytes Long
int count = 8;
if (bytes.length - offset < 8) {
    count = bytes.length - offset;
long num = 0;
byte high = bytes[offset + count - 1];
if (high < 0) {
    num = -1;
...
longbytes2Long(final byte[] b)
bytes Long
return bytes2Long(b, 0);
longbytes2long(final byte[] bytes, final int start)
Setup a long from an array of bytes.
int i = 0;
final int length = 4;
int count = 0;
final byte[] tmp = new byte[length];
for (i = start; i < (start + length); i++) {
    tmp[count] = bytes[i];
    count++;
long accum = 0;
i = 0;
for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
    accum |= ((long) (tmp[i] & 0xff)) << shiftBy;
    i++;
return accum;
longbytesToLong(byte a, byte b, byte c, byte d)
bytes To Long
return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));
longbytesToLong(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h, boolean swapBytes)
Concatenate eight bytes to a 64-bit int value.
if (swapBytes) {
    return ((a & 0xffl)) + ((b & 0xffl) << 8) + ((c & 0xffl) << 16) + ((d & 0xffl) << 24)
            + ((e & 0xffl) << 32) + ((f & 0xffl) << 40) + ((g & 0xffl) << 48) + ((h & 0xffl) << 56);
} else {
    return ((a & 0xffl) << 56) + ((b & 0xffl) << 48) + ((c & 0xffl) << 40) + ((d & 0xffl) << 32)
            + ((e & 0xffl) << 24) + ((f & 0xffl) << 16) + ((g & 0xffl) << 8) + ((h & 0xffl));
longbytesToLong(byte[] a, int ao)
bytes To Long
return ((a[ao] & 0xffL) << 56) | ((a[ao + 1] & 0xffL) << 48) | ((a[ao + 2] & 0xffL) << 40)
        | ((a[ao + 3] & 0xffL) << 32) | ((a[ao + 4] & 0xffL) << 24) | ((a[ao + 5] & 0xffL) << 16)
        | ((a[ao + 6] & 0xffL) << 8) | (a[ao + 7] & 0xffL);
longbytesToLong(byte[] arr, int offset)
bytes To Long
long num = (long) ((arr[offset + 0] & 0xFF) << 56) | (long) ((arr[offset + 1] & 0xFF) << 48)
        | (long) ((arr[offset + 2] & 0xFF) << 40) | (long) ((arr[offset + 3] & 0xFF) << 32)
        | (long) ((arr[offset + 4] & 0xFF) << 24) | (long) ((arr[offset + 5] & 0xFF) << 16)
        | (long) ((arr[offset + 6] & 0xFF) << 8) | (long) ((arr[offset + 7] & 0xFF) << 0);
return num;
longbytesToLong(byte[] array, int offset)
Reconstructs a long from bytes that was encoded with #longToBytes(long,byte[],int) .
long value = (long) array[offset + 0] & 0x00000000000000ff;
value = (value << 8) | (array[offset + 1] & 0x00000000000000ff);
value = (value << 8) | (array[offset + 2] & 0x00000000000000ff);
value = (value << 8) | (array[offset + 3] & 0x00000000000000ff);
value = (value << 8) | (array[offset + 4] & 0x00000000000000ff);
value = (value << 8) | (array[offset + 5] & 0x00000000000000ff);
value = (value << 8) | (array[offset + 6] & 0x00000000000000ff);
value = (value << 8) | (array[offset + 7] & 0x00000000000000ff);
...
longbytesToLong(byte[] b)
Converts a byte[] to a long
long l = 0L;
l = b[0];
l = (l << 8) | b[1];
l = (l << 8) | b[2];
l = (l << 8) | b[3];
l = (l << 8) | b[4];
l = (l << 8) | b[5];
l = (l << 8) | b[6];
...
longbytesToLong(byte[] b)
Assemble eight bytes to an long value, make sure that the passed bytes length larger than 8.
assert b.length >= 8;
return ((b[0] & 0xFFL) << 56) + ((b[1] & 0xFFL) << 48) + ((b[2] & 0xFFL) << 40) + ((b[3] & 0xFFL) << 32)
        + ((b[4] & 0xFFL) << 24) + ((b[5] & 0xFFL) << 16) + ((b[6] & 0xFFL) << 8) + ((b[7] & 0xFFL) << 0);