Java Byte Array to Long byte2Long(byte[] bytes, int offset)

Here you can find the source of byte2Long(byte[] bytes, int offset)

Description

byte Long

License

Apache License

Declaration

public static long byte2Long(byte[] bytes, int offset) 

Method Source Code

//package com.java2s;
/*//from   w w  w .j ava  2  s.com
 * Copyright 2015-2017 GenerallyCloud.com
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static long byte2Long(byte[] bytes, int offset) {

        checkLength(bytes, 8, offset);

        long v0 = bytes[offset + 7] & 0xff;
        long v1 = (long) (bytes[offset + 6] & 0xff) << 8 * 1;
        long v2 = (long) (bytes[offset + 5] & 0xff) << 8 * 2;
        long v3 = (long) (bytes[offset + 4] & 0xff) << 8 * 3;
        long v4 = (long) (bytes[offset + 3] & 0xff) << 8 * 4;
        long v5 = (long) (bytes[offset + 2] & 0xff) << 8 * 5;
        long v6 = (long) (bytes[offset + 1] & 0xff) << 8 * 6;
        long v7 = (long) (bytes[offset + 0] & 0xff) << 8 * 7;
        return (v0 | v1 | v2 | v3 | v4 | v5 | v6 | v7);

    }

    private static void checkLength(byte[] bytes, int length, int offset) {

        if (bytes == null) {
            throw new IllegalArgumentException("null");
        }

        if (offset < 0) {
            throw new IllegalArgumentException("invalidate offset " + offset);
        }

        if (bytes.length - offset < length) {
            throw new IllegalArgumentException("invalidate length " + bytes.length);
        }
    }
}

Related

  1. atol(byte[] s, int offset)
  2. bufferToLong(byte[] ioBuffer)
  3. byte2long(byte[] b)
  4. byte2long(byte[] b)
  5. byte2Long(byte[] bytes, int offset)
  6. byte2long(byte[] data)
  7. byte2long(byte[] value, int count)
  8. byte2long(final byte[] v)
  9. bytes2long(byte[] b)