Java Long to Byte Array longToBytes(final long l)

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

Description

Convert a long to a byte array.

License

Open Source License

Parameter

Parameter Description
l the long to convert to byte[]

Return

the byte array representing the long

Declaration

public static byte[] longToBytes(final long l) 

Method Source Code

//package com.java2s;
/*//from w  w  w . j  a v  a 2  s .  c om
 *
 * JMeta - Meta's java implementation
 *
 * Copyright (C) 2013-2015 Pablo Joubert
 * Copyright (C) 2013-2015 Thomas Lavocat
 * Copyright (C) 2013-2015 Nicolas Michon
 *
 * This file is part of JMeta.
 *
 * JMeta is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * JMeta 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * Convert a long to a byte array.
     *
     * @param l the long to convert to byte[]
     * @return the byte array representing the long
     */
    public static byte[] longToBytes(final long l) {
        return longToBytes(l, new byte[8], 0);
    }

    /**
     * Convert a long to a byte array.
     *
     * @param l the long to convert to byte[]
     * @param result the destination array
     * @param offset the offset within the destination array
     * @return the given array
     */
    public static byte[] longToBytes(final long l, final byte[] result, final int offset) {
        result[offset] = (byte) (l >>> 56);
        result[offset + 1] = (byte) (l >>> 48);
        result[offset + 2] = (byte) (l >>> 40);
        result[offset + 3] = (byte) (l >>> 32);
        result[offset + 4] = (byte) (l >>> 24);
        result[offset + 5] = (byte) (l >>> 16);
        result[offset + 6] = (byte) (l >>> 8);
        result[offset + 7] = (byte) (l >>> 0);
        return result;
    }
}

Related

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