Java Long to Byte Array longToByteArray(final long l)

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

Description

Create a byte array from long.
Can be revert with #byteArrayToLong(byte[])

License

Open Source License

Parameter

Parameter Description
l Long to convert

Return

Byte array created

Declaration

public static byte[] longToByteArray(final long l) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   www . jav  a2 s .c o  m*/
     * Create a byte array from long.<br>
     * Can be revert with {@link #byteArrayToLong(byte[])}
     * 
     * @param l
     *           Long to convert
     * @return Byte array created
     */
    public static byte[] longToByteArray(final long l) {
        final byte[] array = new byte[8];

        array[0] = (byte) ((l >> 56) & 0xFF);
        array[1] = (byte) ((l >> 48) & 0xFF);
        array[2] = (byte) ((l >> 40) & 0xFF);
        array[3] = (byte) ((l >> 32) & 0xFF);
        array[4] = (byte) ((l >> 24) & 0xFF);
        array[5] = (byte) ((l >> 16) & 0xFF);
        array[6] = (byte) ((l >> 8) & 0xFF);
        array[7] = (byte) (l & 0xFF);

        return array;
    }
}

Related

  1. long2bytes(long value, byte[] bytes, int offset)
  2. long2bytes(long x)
  3. long2bytes(long... numbers)
  4. long2bytesBE(long val, byte[] b, int off)
  5. longToByteArr(long v)
  6. longToByteArray(final long v, final byte[] buf, final int offset)
  7. longToByteArray(final long val, final byte[] buf, final int offset)
  8. longToByteArray(final long value, final int size)
  9. longToByteArray(long a, byte[] buf)