Android Byte Array to Long Convert getLong(byte[] buf, int pos, boolean bigEndian)

Here you can find the source of getLong(byte[] buf, int pos, boolean bigEndian)

Description

Convert byte sequence into java long.

Parameter

Parameter Description
buf the source byte array
pos the position of array to convert from
bigEndian true if big endian, false if little endian

Return

short the java long

Declaration

public static final long getLong(byte[] buf, int pos, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**//w  w w  .jav  a  2  s .c o  m
     * Convert byte sequence into java long from first 8 bytes
     *
     * @param buf the source byte array
     * @param bigEndian true if big endian, false if little endian
     * @return short the java long
     */
    public static final long getLong(byte[] buf, boolean bigEndian) {
        return getLong(buf, 0, bigEndian);
    }

    /**
     * Convert byte sequence into java long.
     *
     * @param buf the source byte array
     * @param pos the position of array to convert from
     * @param bigEndian true if big endian, false if little endian
     * @return short the java long
     */
    public static final long getLong(byte[] buf, int pos, boolean bigEndian) {
        if (bigEndian) {
            return (((long) buf[pos + 7]) & 0xFF)
                    | ((((long) buf[pos + 6]) & 0xFF) << 8)
                    | ((((long) buf[pos + 5]) & 0xFF) << 16)
                    | ((((long) buf[pos + 4]) & 0xFF) << 24)
                    | ((((long) buf[pos + 3]) & 0xFF) << 32)
                    | ((((long) buf[pos + 2]) & 0xFF) << 40)
                    | ((((long) buf[pos + 1]) & 0xFF) << 48)
                    | ((((long) buf[pos]) & 0xFF) << 56);
        } else {
            return (((long) buf[pos]) & 0xFF)
                    | ((((long) buf[pos + 1]) & 0xFF) << 8)
                    | ((((long) buf[pos + 2]) & 0xFF) << 16)
                    | ((((long) buf[pos + 3]) & 0xFF) << 24)
                    | ((((long) buf[pos + 4]) & 0xFF) << 32)
                    | ((((long) buf[pos + 5]) & 0xFF) << 40)
                    | ((((long) buf[pos + 6]) & 0xFF) << 48)
                    | ((((long) buf[pos + 7]) & 0xFF) << 56);
        }
    }
}

Related

  1. bytesToLong(byte[] bytes, int start)
  2. bytes2ulong(byte[] bytes, int from, int to)
  3. getLong(byte[] bb, int index)
  4. getLong(byte[] bb, int index)
  5. getLong(byte[] buf, boolean bigEndian)
  6. getLong(byte[] bytes)
  7. getLong4(byte[] b, int offset)
  8. getLong5(byte[] b, int offset)
  9. getLongFromByteArray(final byte[] bytes)