Returns the big-endian long value whose byte representation is the 8 bytes of bytes starting offset. - Java java.lang

Java examples for java.lang:long

Description

Returns the big-endian long value whose byte representation is the 8 bytes of bytes starting offset.

Demo Code

/*//from   ww w  .ja  va 2  s.com
 * Copyright (c) 2011-2012 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE file for licensing information.
 *
 * Derived from the code copyrighted and licensed as follows:
 * 
 * Copyright (c) Members of the EGEE Collaboration. 2004.
 * See http://www.eu-egee.org/partners/ for details on the copyright
 * holders.
 * 
 * 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.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int offset = 2;
        System.out.println(getLong(bytes, offset));
    }

    /**
     * Returns the big-endian {@code long} value whose byte representation
     * is the 8 bytes of <code>bytes</code> staring <code>offset</code>.
     * 
     * @param bytes
     * @param offset
     * @return long value
     */
    private static long getLong(byte[] bytes, int offset) {
        return (bytes[offset] & 0xFFL) << 56
                | (bytes[offset + 1] & 0xFFL) << 48
                | (bytes[offset + 2] & 0xFFL) << 40
                | (bytes[offset + 3] & 0xFFL) << 32
                | (bytes[offset + 4] & 0xFFL) << 24
                | (bytes[offset + 5] & 0xFFL) << 16
                | (bytes[offset + 6] & 0xFFL) << 8
                | (bytes[offset + 7] & 0xFFL);
    }
}

Related Tutorials