Java Utililty Methods Long to Byte Array

List of utility methods to do Long to Byte Array

Description

The list of methods to do Long to Byte Array are organized into topic(s).

Method

byte[]longToBytes(long n)
long To Bytes
return longToBytes(n, new byte[8], 0);
byte[]longToBytes(long n)
Converts a long value to a cooresponding 8-byte array starting with the most significant byte.
byte[] b = new byte[8];
for (int i = 0; i < 64; i += 8) {
    b[i >> 3] = (byte) ((n >> (56 - i)) & 0xff);
return b;
voidlongToBytes(long n, byte b[], int offset)
long To Bytes
for (int i = 0; i < 8; i++) {
    b[offset + i] = (byte) (n & 0xff);
    n = n >> 8;
byte[]longToBytes(long num)
long To Bytes
byte[] bytes = new byte[8];
for (int i = 0; i < 8; i++) {
    bytes[i] = (byte) (0xff & (num >> (i * 8)));
return bytes;
byte[]longToBytes(long num, byte[] data, int index)
long To Bytes
for (int i = 0; i < 8; i++) {
    data[index + i] = (byte) (num >> (56 - i * 8));
return data;
byte[]longToBytes(long number)
Converts a long number to a byte array
byte[] ret = new byte[8];
ret[0] = (byte) ((number >>> 8 * 7) & 0xFF);
ret[1] = (byte) ((number >> 8 * 6) & 0xFF);
ret[2] = (byte) ((number >> 8 * 5) & 0xFF);
ret[3] = (byte) ((number >> 8 * 4) & 0xFF);
ret[4] = (byte) ((number >>> 8 * 3) & 0xFF);
ret[5] = (byte) ((number >> 8 * 2) & 0xFF);
ret[6] = (byte) ((number >> 8 * 1) & 0xFF);
...
byte[]longToBytes(long v)
long To Bytes
byte[] data = new byte[8];
int offset = 0;
data[offset++] = (byte) ((v >> 56) & 0xFF);
data[offset++] = (byte) ((v >> 48) & 0xFF);
data[offset++] = (byte) ((v >> 40) & 0xFF);
data[offset++] = (byte) ((v >> 32) & 0xFF);
data[offset++] = (byte) ((v >> 24) & 0xFF);
data[offset++] = (byte) ((v >> 16) & 0xFF);
...
byte[]longToBytes(long v)
Javascript doesn't support long data type, so it has to be converted to double before converting it to byte[].
return doubleToBytes((double) v);
voidlongToBytes(long v, byte[] b)
long To Bytes
assert b.length >= 8;
b[0] = (byte) ((v >>> 56) & 0xFF);
b[1] = (byte) ((v >>> 48) & 0xFF);
b[2] = (byte) ((v >>> 40) & 0xFF);
b[3] = (byte) ((v >>> 32) & 0xFF);
b[4] = (byte) ((v >>> 24) & 0xFF);
b[5] = (byte) ((v >>> 16) & 0xFF);
b[6] = (byte) ((v >>> 8) & 0xFF);
...
voidlongToBytes(long v, byte[] bytes)
long To Bytes
longToBytes(v, bytes, 0);