Android Byte Array to Short Convert getShort(byte[] buf, boolean bigEndian)

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

Description

Convert byte sequence into java short from first 2 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 short getShort(byte[] buf, boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w ww .j  a v a2  s  .  com
     * Convert byte sequence into java short from first 2 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 short getShort(byte[] buf, boolean bigEndian) {
        return getShort(buf, 0, bigEndian);
    }

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

Related

  1. bytesToShort(byte[] b)
  2. bytesToShort(byte[] b)
  3. bytesToShort(byte[] bytes)
  4. getShort(byte[] b, int index)
  5. getShort(byte[] b, int index)
  6. getShort(byte[] buf, int pos, boolean bigEndian)
  7. getShort(byte[] bytes)
  8. toShort(byte[] b, int pos)
  9. toShort(byte[] data)