Java Long Number Create toLong(byte[] buf, int off)

Here you can find the source of toLong(byte[] buf, int off)

Description

Converts 8 bytes to a long at the specified offset in the given byte array.

License

Apache License

Parameter

Parameter Description
buf the byte array containing the 8 bytes to be converted to a <code>long</code>.
off offset in the byte array

Return

the long value of the 8 bytes.

Declaration

public static long toLong(byte[] buf, int off) 

Method Source Code

//package com.java2s;
/*/*from ww  w. j  a v a 2  s.  c o m*/
 * Copyright 1999-2010 University of Chicago
 *
 * 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 {
    /**
     * Converts 8 bytes to a <code>long</code> at the
     * specified offset in the given byte array.
     *
     * @param buf the byte array containing the 8 bytes
     *        to be converted to a <code>long</code>.
     * @param off offset in the byte array
     * @return the <code>long</code> value of the 8 bytes.
     */
    public static long toLong(byte[] buf, int off) {
        return ((long) (toInt(buf, off)) << 32) + (toInt(buf, off + 4) & 0xFFFFFFFFL);
    }

    /**
     * Converts 4 bytes to an <code>int</code> at
     * the specified offset in the given byte array.
     *
     * @param buf the byte array containing the 4 bytes
     *        to be converted to an <code>int</code>.
     * @param off offset in the byte array
     * @return the <code>int</code> value of the 4 bytes.
     */
    public static int toInt(byte[] buf, int off) {
        int lg = (buf[off] & 0xff) << 24;
        lg |= (buf[off + 1] & 0xff) << 16;
        lg |= (buf[off + 2] & 0xff) << 8;
        lg |= (buf[off + 3] & 0xff);
        return lg;
    }
}

Related

  1. toLong(byte[] b)
  2. toLong(byte[] b)
  3. toLong(byte[] b, int off, boolean bigEndian)
  4. toLong(byte[] buf)
  5. toLong(byte[] buf)
  6. toLong(byte[] buf, int pos)
  7. toLong(byte[] byteArray)
  8. toLong(byte[] bytes)
  9. toLong(byte[] bytes)