Android Byte Array to Int Convert getInt(byte[] buf, boolean bigEndian)

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

Description

Convert byte sequence into java short from first 4 bytes

Parameter

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

Return

short the java short

Declaration

public static final int getInt(byte[] buf, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   ww w. j  av a  2  s  .  com*/
     * Convert byte sequence into java short from first 4 bytes
     *
     * @param buf the source byte array
     * @param bigEndian true if big endian, false if little endian
     * @return short the java short
     */
    public static final int getInt(byte[] buf, boolean bigEndian) {
        return getInt(buf, 0, bigEndian);
    }

    /**
     * Convert byte sequence into java int.
     *
     * @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 int
     */
    public static final int getInt(byte[] buf, int pos, boolean bigEndian) {
        if (bigEndian) {
            return ((buf[pos] & 0xff) << 24)
                    | ((buf[pos + 1] & 0xff) << 16)
                    | ((buf[pos + 2] & 0xff) << 8) | (buf[pos + 3] & 0xff);
        } else {
            return ((buf[pos + 3] & 0xff) << 24)
                    | ((buf[pos + 2] & 0xff) << 16)
                    | ((buf[pos + 1] & 0xff) << 8) | (buf[pos] & 0xff);
        }
    }
}

Related

  1. bytesBE2ints(byte[] b)
  2. getInt(byte b)
  3. getInt(byte[] bb, int index)
  4. getInt(byte[] bb, int index)
  5. getInt(byte[] bb, int index)
  6. getInt(byte[] buf, int pos, boolean bigEndian)
  7. getInt(byte[] buffer, int pos, int count)
  8. getInt(byte[] bytes)
  9. getInt(int offset, byte[] fromBytes)