Java Byte Array to Long bytesToLong(final byte[] b)

Here you can find the source of bytesToLong(final byte[] b)

Description

Convert a byte array to long.

License

Open Source License

Parameter

Parameter Description
b the byte[] to convert to long

Return

the long value

Declaration

public static long bytesToLong(final byte[] b) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  .  j ava2  s.c o  m*/
 *
 * JMeta - Meta's java implementation
 *
 * Copyright (C) 2013-2015 Pablo Joubert
 * Copyright (C) 2013-2015 Thomas Lavocat
 * Copyright (C) 2013-2015 Nicolas Michon
 *
 * This file is part of JMeta.
 *
 * JMeta is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * JMeta 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * Convert a byte array to long.
     *
     * @param b the byte[] to convert to long
     * @return the long value
     */
    public static long bytesToLong(final byte[] b) {
        return bytesToLong(b, 0);
    }

    /**
     * Convert a byte array to long.
     *
     * Only 8 bytes are read from the given array starting at offset.
     *
     * @param b the array to convert to long
     * @param offset the offset in the array
     * @return the long value
     */
    public static long bytesToLong(final byte[] b, final int offset) {
        long result = 0;
        for (int i = offset; i < Long.BYTES; i++) {
            result <<= Long.BYTES;
            result |= (b[i] & 0xFF);
        }
        return result;
    }
}

Related

  1. bytesToLong(byte[] data)
  2. bytesToLong(byte[] data, int offset)
  3. bytesToLong(byte[] inbytes, int shift)
  4. bytesToLong(byte[] longBytes)
  5. bytesToLong(byte[] p, int offset)
  6. bytesToLong(final byte[] bytes)
  7. bytesToLong(final byte[] bytes)
  8. bytesToLong(final byte[] data, final int offset)
  9. bytesToLongLE(byte[] bytes, int off)