Java Short Number Create toShort(byte[] bytes, int offset)

Here you can find the source of toShort(byte[] bytes, int offset)

Description

Convert a byte array to a short value

License

Open Source License

Parameter

Parameter Description
bytes a parameter
offset a parameter

Return

the short value corresponding to the 2 first bytes of the byte array

Declaration

public static short toShort(byte[] bytes, int offset) 

Method Source Code

//package com.java2s;
/*//from ww  w  . j a v a  2  s .c  o m
 * This file is part of org.kalmeo.util.
 * 
 * org.kalmeo.util 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.
 * 
 * org.kalmeo.util 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 org.kalmeo.util.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 17 janv. 08
 * Copyright (c) Kalmeo 2007-2008. All rights reserved.
 * http://www.kalmeo.org
 */

public class Main {
    /**
     * Convert a byte array to a short value
     * 
     * @param bytes
     * @param offset
     * @return the short value corresponding to the 2 first bytes of the byte
     *         array
     */
    public static short toShort(byte[] bytes, int offset) {
        short value = 0;
        if (bytes != null && bytes.length >= 2) {
            value += (0x000000FF & bytes[offset]) << 8;
            value += (0x000000FF & bytes[offset + 1]);
        }
        return value;
    }
}

Related

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