Java Short Number Create toShort(byte[] bytes, boolean bigEndian)

Here you can find the source of toShort(byte[] bytes, boolean bigEndian)

Description

Converts a byte array to a signed short value.

License

Open Source License

Declaration

static public short toShort(byte[] bytes, boolean bigEndian) 

Method Source Code

//package com.java2s;
/*/*  w w w  .  j  av  a  2 s .co m*/
 * Copyright 1999-2004 Carnegie Mellon University.  
 * Portions Copyright 2002-2004 Sun Microsystems, Inc.  
 * Portions Copyright 2002-2004 Mitsubishi Electric Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

public class Main {
    /** Converts a byte array to a signed short value. */
    static public short toShort(byte[] bytes, boolean bigEndian) {
        if (bytes.length == 1) {
            return bytes[0];
        } else if (bigEndian) {
            return (short) ((bytes[0] << 8) | (0xff & bytes[1]));
        } else {
            return (short) ((bytes[1] << 8) | (0xff & bytes[0]));
        }
    }
}

Related

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