Java Byte Array to Short bytesToShort(byte a, byte b, boolean swapBytes)

Here you can find the source of bytesToShort(byte a, byte b, boolean swapBytes)

Description

Concatenate two bytes to a short integer value.

License

Open Source License

Parameter

Parameter Description
a high order byte
b low order byte
swapBytes reverse the roles of the first two parameters

Return

short integer representation of the concatenated bytes

Declaration

public static short bytesToShort(byte a, byte b, boolean swapBytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  ww w .  j  a v  a2s.c  o  m
     * Concatenate two bytes to a short integer value.  Accepts a high and low byte to be
     * converted to a 16-bit integer.  if swapBytes is true, then <b>b</b> becomes
     * the high order byte.
     * @param a high order byte
     * @param b low order byte
     * @param swapBytes reverse the roles of the first two parameters
     * @return short integer representation of the concatenated bytes
     */
    public static short bytesToShort(byte a, byte b, boolean swapBytes) {
        if (swapBytes) {
            return (short) ((a & 0xff) + (b & 0xff) << 8);
        } else {
            return (short) (((a & 0xff) << 8) + (b & 0xff));
        }
    }
}

Related

  1. bytes2Short(byte[] bytes)
  2. bytes2short(byte[] bytes, int offset, boolean bigEndian)
  3. bytes2Short(byte[] input)
  4. bytes2short(byte[] src)
  5. bytesToShort(byte A, byte B)
  6. bytesToShort(byte b1, byte b2)
  7. bytesToShort(byte byte1, byte byte2)
  8. bytesToShort(byte hiByte, byte loByte)
  9. bytesToShort(byte[] buf)