Here you can find the source of bytesToShort(byte a, byte b, boolean swapBytes)
Parameter | Description |
---|---|
a | high order byte |
b | low order byte |
swapBytes | reverse the roles of the first two parameters |
public static short bytesToShort(byte a, byte b, boolean swapBytes)
//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)); } } }