Java Long to Byte Array longToBytes(long val)

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

Description

Converts a long val into an array of bytes.

License

Open Source License

Return

The byte[] representation of the long value.

Declaration

public static byte[] longToBytes(long val) 

Method Source Code

//package com.java2s;

public class Main {
    /** Converts a long {@code val} into an array of bytes.
    */*from   w  w  w.ja va2s .c om*/
    *@return The {@code byte[]} representation of the long value.
    */
    public static byte[] longToBytes(long val) {
        byte[] byteArr = new byte[8];

        for (int i = 0; i < 8; i++) {
            byte nextByte = (byte) ((val >> i * 8) & 0xff);
            byteArr[i] = nextByte;
        }

        return byteArr;
    }
}

Related

  1. longToBytes(long v)
  2. longToBytes(long v)
  3. longToBytes(long v, byte[] b)
  4. longToBytes(long v, byte[] bytes)
  5. longToBytes(long v, final byte[] arr)
  6. longToBytes(long val)
  7. longToBytes(long value)
  8. longToBytes(long value, byte[] array, int offset)
  9. longToBytes(long value, byte[] buffer, int bufferStartPosition)