Java Short Number Create toShort(byte[] data)

Here you can find the source of toShort(byte[] data)

Description

Translates a network order byte representation of a short back into a java primitive.

License

Open Source License

Parameter

Parameter Description
data network bytes representing a short

Return

java primitive from the network bytes

Declaration

public static int toShort(byte[] data) 

Method Source Code

//package com.java2s;
/*//w  ww  .  j av a  2s  . c om
 * Copyright 2009 Charles Chappell.
 *
 * This file is part of IcedJava.
 *
 * IcedJava is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * IcedJava 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with IcedJava.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static final int SHORTSIZE = 2;

    /**
     * Translates a network order byte representation of a short back into a 
     * java primitive.
     * @param data network bytes representing a short
     * @return java primitive from the network bytes
     */
    public static int toShort(byte[] data) {
        return toShort(data, 0);
    }

    /**
     * Translates a network order byte representation of a short back into a 
     * java primitive.
     * @param data network bytes representing a short
     * @param offset offset into the array to start reading from
     * @return java primitive from the network bytes
     */
    public static int toShort(byte[] data, int offset) {
        int number = 0;
        for (int i = 0; i < SHORTSIZE; i++) {
            number = (short) (number << 8 | (0x00ff & data[offset + i]));
        }
        return number & 0x0000ffff;
    }
}

Related

  1. toShort(byte[] bytes)
  2. toShort(byte[] bytes, boolean bigEndian)
  3. toShort(byte[] bytes, int from)
  4. toShort(byte[] bytes, int offset)
  5. toShort(byte[] bytes, int offset)
  6. toShort(byte[] input)
  7. toShort(byte[] key)
  8. toShort(byte[] readBuffer, int o)
  9. toShort(byte[] si, boolean isReverseOrder)