Java Long Number From longFromBytes(byte[] bytes, int index)

Here you can find the source of longFromBytes(byte[] bytes, int index)

Description

Unsigned Long from 8 bytes

License

Open Source License

Parameter

Parameter Description
bytes an 8-byte array
index the index offset into the byte array to sample

Return

Unsigned Long

Declaration

public static long longFromBytes(byte[] bytes, int index) 

Method Source Code

//package com.java2s;
/*//from w  w w  . j av a2  s . c o  m
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Unsigned Long from 8 bytes
     *
     * @param bytes an 8-byte array
     * @param index the index offset into the byte array to sample
     * @return Unsigned Long
     */
    public static long longFromBytes(byte[] bytes, int index) {
        if ((bytes == null) || (bytes.length < 7)) {
            return -1;
        }
        int idx = index;
        long val = 0;
        for (int i = 0; i < 8; i++) {
            if (i < 7) {
                val |= (((long) bytes[idx + i] & 0x000000ff) << (64 - ((i + 1) * 8)));
            } else {
                val |= ((long) bytes[idx + i] & 0x000000ff);
            }
        }
        return val;
    }
}

Related

  1. longFromByteArray(byte[] bytes)
  2. longFromByteArray(final byte[] buf, final int offset)
  3. longFromBytes(byte b8, byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1)
  4. longFrombytes(byte[] b, int pos)
  5. longFromBytes(byte[] buffer, int offset)
  6. longFromBytes(byte[] bytes, int offset)
  7. longFromDB(String dbLong)
  8. longFromLex(byte[] bytes)
  9. longFromObject(Object o)