Java Long Number Create toLong(byte[] b)

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

Description

Build a long from first 8 bytes of the array.

License

Open Source License

Parameter

Parameter Description
b The byte[] to convert.

Return

A long.

Declaration

public static long toLong(byte[] b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.//from w w w  .  ja v a2  s  . c  om
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Build a long from first 8 bytes of the array.
     * 
     * @param b The byte[] to convert.
     * @return A long.
     */
    public static long toLong(byte[] b) {
        if (b.length != 8) {
            throw new IllegalArgumentException();
        }

        return ((long) b[7] & 0xFF) + (((long) b[6] & 0xFF) << 8) + (((long) b[5] & 0xFF) << 16)
                + (((long) b[4] & 0xFF) << 24) + (((long) b[3] & 0xFF) << 32) + (((long) b[2] & 0xFF) << 40)
                + (((long) b[1] & 0xFF) << 48) + (((long) b[0] & 0xFF) << 56);
    }
}

Related

  1. toLong(byte byte7, byte byte6, byte byte5, byte byte4, byte byte3, byte byte2, byte byte1, byte byte0)
  2. toLong(byte... b)
  3. toLong(byte... b)
  4. toLong(byte[] arr)
  5. toLong(byte[] array, int offset)
  6. toLong(byte[] b)
  7. toLong(byte[] b)
  8. toLong(byte[] b)
  9. toLong(byte[] b)