Android Byte Array to Long Convert bytesToLong(byte[] bytes)

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

Description

Converts a byte[] of unsigned bytes in big-endian order to a long.

License

Open Source License

Parameter

Parameter Description
bytes - A byte[] containing the bytes to convert.

Return

A long containing the equivalent signed value of the given bytes.

Declaration

public static long bytesToLong(byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w w w.  ja va2  s  .c  o  m
     * Converts a byte[] of unsigned bytes in big-endian order to a long. 
     * 
     * @param bytes - A byte[] containing the bytes to convert.
     * 
     * @return A long containing the equivalent signed value of the given bytes.
     */
    public static long bytesToLong(byte[] bytes) {
        long l = 0;
        l |= (bytes[0] & 0xFFL) << 56;
        l |= (bytes[1] & 0xFFL) << 48;
        l |= (bytes[2] & 0xFFL) << 40;
        l |= (bytes[3] & 0xFFL) << 32;
        l |= (bytes[4] & 0xFFL) << 24;
        l |= (bytes[5] & 0xFFL) << 16;
        l |= (bytes[6] & 0xFFL) << 8;
        l |= (bytes[7] & 0xFFL);
        return l;
    }
}

Related

  1. bytesBE2long(byte[] b, int off)
  2. bytesBE2longs(byte[] b)
  3. byteToLong(byte[] b)
  4. bytesLE2long(byte[] b, int off)
  5. bytesLE2longs(byte[] b)
  6. bytesToLong(byte[] bytes)
  7. bytesToLong(byte[] bytes, int start)
  8. bytes2ulong(byte[] bytes, int from, int to)
  9. getLong(byte[] bb, int index)