Java Long Number Create toLong(final byte[] b)

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

Description

Converts the bytes in an array into the corresponding long value.

License

Open Source License

Parameter

Parameter Description
b The array with the bytes to convert

Return

The long value of the bytes.

Declaration

public final static long toLong(final byte[] b) 

Method Source Code

//package com.java2s;
/*/*  ww  w .  j a v a  2s .c o m*/
 * Copyright (C) 2014 Russell Smith.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

public class Main {
    /**
     * Converts the bytes in an array into the corresponding long value.  The 
     * bytes need to be in the network byte order.
     * 
     * @param b The array with the bytes to convert
     * @return The long value of the bytes.
     */
    public final static long toLong(final byte[] b) {
        return toLong(b, 0);
    }

    /**
     * Converts the bytes in an array into the corresponding long value.  The 
     * bytes need to be in the network byte order.
     * 
     * @param b The array with the bytes to convert
     * @param offset The offset into the array for the bytes to convert.
     * @return The long value of the bytes.
     */
    public final static int toLong(final byte[] b, final int offset) {
        int result;

        result = b[offset];
        result |= (long) (b[offset + 1]) << 8;
        result |= (long) (b[offset + 2]) << 16;
        result |= (long) (b[offset + 3]) << 24;
        result |= (long) (b[offset + 4]) << 32;
        result |= (long) (b[offset + 5]) << 40;
        result |= (long) (b[offset + 6]) << 48;
        result |= (long) (b[offset + 7]) << 56;

        return result;
    }
}

Related

  1. toLong(byte[] vint)
  2. toLong(double val)
  3. toLong(double[] in, long[] out)
  4. toLong(double[] v)
  5. toLong(final byte[] array, final int offset, final int length)
  6. toLong(final byte[] b, final int offset)
  7. toLong(final byte[] bytes, final int offset)
  8. toLong(final byte[] data)
  9. toLong(final byte[] data)