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

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

Description

Convert byte sequence into java long from first 8 bytes

Parameter

Parameter Description
buf the source byte array
bigEndian true if big endian, false if little endian

Return

short the java long

Declaration

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

Method Source Code

//package com.java2s;

public class Main {
    /**/*  w  ww  .  j  a v  a2  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)
  2. bytesToLong(byte[] bytes, int start)
  3. bytes2ulong(byte[] bytes, int from, int to)
  4. getLong(byte[] bb, int index)
  5. getLong(byte[] bb, int index)
  6. getLong(byte[] buf, int pos, boolean bigEndian)
  7. getLong(byte[] bytes)
  8. getLong4(byte[] b, int offset)
  9. getLong5(byte[] b, int offset)