0

In order to see a byte array of 8 bytes as a long, I did this:

public static long byteArrayToLongBE(byte[] b) {
        long r = 0;
        r |= Byte.toUnsignedInt(b[0]);
        r |= Byte.toUnsignedInt(b[1])<<8;
        r |= Byte.toUnsignedInt(b[2])<<16;
        r |= Byte.toUnsignedLong(b[3]) <<24;
        r |= Byte.toUnsignedLong(b[4])<<32;
        r |= Byte.toUnsignedLong(b[5])<<40;
        r |= Byte.toUnsignedLong(b[6])<<48;
        r |= Byte.toUnsignedLong(b[7])<<56;
        return r;
    }

while on C++ I would just cast the array: (uint64_t*) byte_array, which is much much faster.

What is the fastest way to convert a slice of an array to a long number in Java?

Guerlando OCs
  • 162
  • 1
  • 29
  • 87

0 Answers0