Java Long Number Create toLong(byte[] bytes, boolean bigEndian)

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

Description

Convert a byte array to a long integer.

License

Open Source License

Parameter

Parameter Description
bytes Byte array to be converted.
bigEndian true when input should be treated as Big Endian, false for Little Endian.

Return

long

Declaration

public static long toLong(byte[] bytes, boolean bigEndian) 

Method Source Code

//package com.java2s;
/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH./*w ww. ja  va 2s . co 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 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. toLong(byte[] bytes)
  2. toLong(byte[] bytes)
  3. toLong(byte[] bytes)
  4. toLong(byte[] bytes)
  5. toLong(byte[] bytes)
  6. toLong(byte[] bytes, int index)
  7. toLong(byte[] bytes, int index)
  8. toLong(byte[] bytes, int offset)
  9. toLong(byte[] bytes, int offset)