Java Long to Byte Array longToBytes(long l, byte[] arr, int startIdx)

Here you can find the source of longToBytes(long l, byte[] arr, int startIdx)

Description

Unsigned Long to 8 bytes

License

Open Source License

Parameter

Parameter Description
l a Java long
arr a byte array, 8-bytes or longer
startIdx the start index into the array where the LONG should be written

Declaration

public static void longToBytes(long l, byte[] arr, int startIdx) 

Method Source Code

//package com.java2s;
/*/*from  w w w . jav  a 2 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 to 8 bytes
     *
     * @param l        a Java long
     * @param arr      a byte array, 8-bytes or longer
     * @param startIdx the start index into the array where the LONG should be
     *                 written
     */
    public static void longToBytes(long l, byte[] arr, int startIdx) {
        if (arr == null) {
            return;
        }
        int idx = startIdx;
        arr[idx++] = (byte) ((l >>> 56) & 0x00000000000000ff);
        arr[idx++] = (byte) ((l >>> 48) & 0x00000000000000ff);
        arr[idx++] = (byte) ((l >>> 40) & 0x00000000000000ff);
        arr[idx++] = (byte) ((l >>> 32) & 0x00000000000000ff);
        arr[idx++] = (byte) ((l >>> 24) & 0x00000000000000ff);
        arr[idx++] = (byte) ((l >>> 16) & 0x00000000000000ff);
        arr[idx++] = (byte) ((l >>> 8) & 0x00000000000000ff);
        arr[idx] = (byte) (l & 0x00000000000000ff);
    }
}

Related

  1. longToBytes(long data)
  2. longToBytes(long k, byte[] b, int i)
  3. longToBytes(long l)
  4. longToBytes(long l)
  5. longToBytes(long l)
  6. longToBytes(long l, byte[] b)
  7. longToBytes(long l, byte[] bytes, int offset)
  8. longToBytes(long l, byte[] data, int[] offset)
  9. longToBytes(long l, byte[] result)