Java Short Number Create toShort(final byte[] b)

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

Description

Converts the bytes in an array into the corresponding short value.

License

Open Source License

Parameter

Parameter Description
b The array with the bytes to convert

Return

The short value of the bytes.

Declaration

public final static short toShort(final byte[] b) 

Method Source Code

//package com.java2s;
/*/*  w w w .j  av a2 s.c o  m*/
 * Copyright (C) 2014 Russell Smith.
 *
 * This library 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.0 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

public class Main {
    /**
     * Converts the bytes in an array into the corresponding short value.  The 
     * bytes need to be in the network byte order.
     * 
     * @param b The array with the bytes to convert
     * @return The short value of the bytes.
     */
    public final static short toShort(final byte[] b) {
        return toShort(b, 0);
    }

    /**
     * Converts the bytes in an array into the corresponding short value.  The 
     * bytes need to be in the network byte order.
     * 
     * @param b The array with the bytes to convert
     * @param offset The offset into the array for the bytes to convert.
     * @return The short value of the bytes.
     */
    public final static short toShort(final byte[] b, final int offset) {
        short result;

        result = b[offset];
        result |= (short) (b[offset + 1]) << 8;

        return result;
    }
}

Related

  1. toShort(byte[] si, boolean isReverseOrder)
  2. toShort(byte[] src)
  3. toShort(byte[] value)
  4. toShort(char[] bytes, boolean le)
  5. toShort(final byte[] b)
  6. toShort(final byte[] buffer, final int offset, final int length)
  7. toShort(final byte[] inputBytes, int offset)
  8. toShort(final String bitString)
  9. toShort(final String value)