Java Short Number Create toShort(byte msb, byte lsb)

Here you can find the source of toShort(byte msb, byte lsb)

Description

Convert 2 bytes into a short.

License

Open Source License

Parameter

Parameter Description
msb The Most Significant Byte.
lsb The Least Significant Byte.

Return

A short representing the bytes.

Declaration

public static short toShort(byte msb, byte lsb) 

Method Source Code

//package com.java2s;
/*//from   w  ww  .j  a  v  a2 s . co m
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Convert 2 bytes into a short.
     * <p>
     * This converts the 2 bytes into a short. The msb will be the high byte (8
     * bits) of the short, and the lsb will be the low byte (8 bits) of the short.
     *
     * @param msb The Most Significant Byte.
     * @param lsb The Least Significant Byte.
     * @return A short representing the bytes.
     */
    public static short toShort(byte msb, byte lsb) {
        return (short) ((0xff00 & (short) (msb << 8)) | (0x00ff & (short) lsb));
    }

    public static short toShort(byte[] data) {
        if ((data == null) || (data.length != 2)) {
            return 0x0;
        }
        return (short) ((0xff & data[0]) << 8 | (0xff & data[1]));
    }
}

Related

  1. toShort(byte a, byte b)
  2. toShort(byte b0, byte b1)
  3. toShort(byte byte0, byte byte1)
  4. toShort(byte mostSignificant, byte leastSignificant)
  5. toShort(byte... b)
  6. toShort(byte... b)
  7. toShort(byte[] b)
  8. toShort(byte[] b)