Java Short Number Create toShort(byte[] bytes)

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

Description

Convert a byte array to a short integer.

License

Open Source License

Parameter

Parameter Description
bytes Byte array to be converted.

Return

short

Declaration

public static short toShort(byte[] bytes) 

Method Source Code

//package com.java2s;
/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.//from  w w  w.  j  a v  a2 s.c  o m
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

public class Main {
    /**
     * Convert a byte array to a short integer.
     * The size of byte array must be between 1 and 2. The input is treated as Big Endian.
     *
     * @param bytes Byte array to be converted.
     * @return short
     */
    public static short toShort(byte[] bytes) {
        return toShort(bytes, true);
    }

    /**
     * Convert a byte array to a short integer.
     * The size of byte array must be between 1 and 2. The endianess of the input is determined by the respective
     * parameter.
     *
     * @param bytes Byte array to be converted.
     * @param bigEndian {@code true} when input should be treated as Big Endian, {@code false} for Little Endian.
     * @return short
     */
    public static short toShort(byte[] bytes, boolean bigEndian) {
        if (bytes.length > 2 || bytes.length < 1) {
            throw new IllegalArgumentException("Size of byte array must be between 1 and 2.");
        }

        return (short) toLong(bytes, bigEndian);
    }

    /**
     * Convert a byte array to a long integer.
     * The size of byte array must be between 1 and 8. The input is treated as Big Endian.
     *
     * @param bytes Byte array to be converted.
     * @return long
     */
    public static long toLong(byte[] bytes) {
        return toLong(bytes, true);
    }

    /**
     * Convert a byte array to a long integer.
     * The size of byte array must be between 1 and 8. The endianess of the input is determined by the respective
     * parameter.
     *
     * @param bytes Byte array to be converted.
     * @param bigEndian {@code true} when input should be treated as Big Endian, {@code false} for Little Endian.
     * @return long
     */
    public static long toLong(byte[] bytes, boolean bigEndian) {
        if (bytes.length > 8 || bytes.length < 1) {
            throw new IllegalArgumentException("Size of byte array must be between 1 and 8.");
        }

        long result = 0;

        if (bigEndian) {
            for (int i = 0; i < bytes.length; i++) {
                result |= ((long) 0xFF & bytes[bytes.length - 1 - i]) << i * 8;
            }
        } else {
            for (int i = 0; i < bytes.length; i++) {
                result |= ((long) 0xFF & bytes[i]) << i * 8;
            }
        }

        return result;
    }
}

Related

  1. toShort(byte[] b, int offset)
  2. toShort(byte[] buf, int pos)
  3. toShort(byte[] byteArray)
  4. toShort(byte[] bytes)
  5. toShort(byte[] bytes)
  6. toShort(byte[] bytes)
  7. toShort(byte[] bytes)
  8. toShort(byte[] bytes, boolean bigEndian)
  9. toShort(byte[] bytes, int from)