Java Integer Create toInteger(byte[] bytes)

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

Description

Convert a byte array to an integer.

License

Open Source License

Parameter

Parameter Description
bytes Byte array to be converted.

Return

int

Declaration

public static int toInteger(byte[] bytes) 

Method Source Code

//package com.java2s;
/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.//from  w w w . j a  v a  2  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 an integer.
     * The size of byte array must be between 1 and 4. The input is treated as Big Endian.
     *
     * @param bytes Byte array to be converted.
     * @return int
     */
    public static int toInteger(byte[] bytes) {
        return toInteger(bytes, true);
    }

    /**
     * Convert a byte array to an integer.
     * The size of byte array must be between 1 and 4. 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 int
     */
    public static int toInteger(byte[] bytes, boolean bigEndian) {
        if (bytes.length > 4 || bytes.length < 1) {
            throw new IllegalArgumentException("Size of byte array must be between 1 and 4.");
        }

        return (int) 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. toInteger(boolean bool)
  2. toInteger(boolean bool)
  3. toInteger(byte b1, byte b2, byte b3, byte b4)
  4. toInteger(byte[] b)
  5. toInteger(byte[] b)
  6. toInteger(byte[] bytes)
  7. toInteger(byte[] bytes, int index)
  8. toInteger(byte[] input)
  9. toInteger(E val)