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

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

Description

Convert byte sequence into java int.

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 int

Declaration

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

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w  w  w  . j  a  va  2  s. c om
     * 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. getInt(byte b)
  2. getInt(byte[] bb, int index)
  3. getInt(byte[] bb, int index)
  4. getInt(byte[] bb, int index)
  5. getInt(byte[] buf, boolean bigEndian)
  6. getInt(byte[] buffer, int pos, int count)
  7. getInt(byte[] bytes)
  8. getInt(int offset, byte[] fromBytes)
  9. getInt2(byte[] b, int offset)