Java Long to Byte Array longToBytes(final long val)

Here you can find the source of longToBytes(final long val)

Description

Writes a big-endian 8-byte long at an offset in the given array.

License

Open Source License

Parameter

Parameter Description
val The value to encode.

Exception

Parameter Description
IndexOutOfBoundsException if the byte array is too small.

Declaration

public static byte[] longToBytes(final long val) 

Method Source Code

//package com.java2s;
/**/*from  w  ww  . j ava  2  s  . c o m*/
 * Copyright (c) 2010 Yahoo! Inc., 2016 YCSB contributors. All rights reserved.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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. See accompanying
 * LICENSE file.
 */

public class Main {
    /**
     * Writes a big-endian 8-byte long at an offset in the given array.
     * @param val The value to encode.
     * @throws IndexOutOfBoundsException if the byte array is too small.
     */
    public static byte[] longToBytes(final long val) {
        final byte[] bytes = new byte[8];
        bytes[0] = (byte) (val >>> 56);
        bytes[1] = (byte) (val >>> 48);
        bytes[2] = (byte) (val >>> 40);
        bytes[3] = (byte) (val >>> 32);
        bytes[4] = (byte) (val >>> 24);
        bytes[5] = (byte) (val >>> 16);
        bytes[6] = (byte) (val >>> 8);
        bytes[7] = (byte) (val >>> 0);
        return bytes;
    }
}

Related

  1. longToByteBounds(long value)
  2. longToBytes(byte[] arr, int offset, long num)
  3. longToBytes(final long aLong)
  4. longToBytes(final long l)
  5. longToBytes(final long l)
  6. longToBytes(final long value)
  7. longToBytes(long data)
  8. longToBytes(long k, byte[] b, int i)
  9. longToBytes(long l)