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

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

Description

Assemble eight bytes to an long value, make sure that the passed bytes length larger than 8.

License

Open Source License

Parameter

Parameter Description
bytes a parameter

Return

int value of bytes

Declaration

public final static long bytesToLong(byte[] b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004,2009 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  w  w. j  a  v a2  s.  c o  m*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Assemble eight bytes to an long value, make sure that the passed bytes
     * length larger than 8.
     * 
     * @param bytes
     * @return int value of bytes
     */
    public final static long bytesToLong(byte[] b) {
        assert b.length >= 8;
        return ((b[0] & 0xFFL) << 56) + ((b[1] & 0xFFL) << 48) + ((b[2] & 0xFFL) << 40) + ((b[3] & 0xFFL) << 32)
                + ((b[4] & 0xFFL) << 24) + ((b[5] & 0xFFL) << 16) + ((b[6] & 0xFFL) << 8) + ((b[7] & 0xFFL) << 0);

    }

    public final static long bytesToLong(byte[] b, int off) {
        assert b.length - off >= 8;
        return ((b[off++] & 0xFFL) << 56) + ((b[off++] & 0xFFL) << 48) + ((b[off++] & 0xFFL) << 40)
                + ((b[off++] & 0xFFL) << 32) + ((b[off++] & 0xFFL) << 24) + ((b[off++] & 0xFFL) << 16)
                + ((b[off++] & 0xFFL) << 8) + ((b[off] & 0xFFL) << 0);
    }
}

Related

  1. bytesToLong(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h, boolean swapBytes)
  2. bytesToLong(byte[] a, int ao)
  3. bytesToLong(byte[] arr, int offset)
  4. bytesToLong(byte[] array, int offset)
  5. bytesToLong(byte[] b)
  6. bytesToLong(byte[] b)
  7. bytesToLong(byte[] bs)
  8. bytesToLong(byte[] buf, int offset, int length, boolean asc)
  9. bytesToLong(byte[] bytes)