Java Long to Byte Array longToBytes(long val)

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

Description

Convert long value to byte array.

License

Apache License

Parameter

Parameter Description
val the long value

Return

the byte array of this value

Declaration

public static byte[] longToBytes(long val) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w w w. jav a  2  s.c om*/
     * Convert long value to byte array.
     *
     * @param val the long value
     * @return the byte array of this value
     */
    public static byte[] longToBytes(long val) {
        byte[] b = new byte[8];
        for (int i = 7; i > 0; i--) {
            b[i] = (byte) val;
            val >>>= 8;
        }
        b[0] = (byte) val;
        return b;
    }
}

Related

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