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 v, final byte[] arr)
Returns a Little-Endian byte array extracted from the given long.
for (int i = 0; i < 8; i++) {
    arr[i] = (byte) (v & 0XFFL);
    v >>>= 8;
return arr;
byte[]longToBytes(long val)
Converts a long val into an array of bytes.
byte[] byteArr = new byte[8];
for (int i = 0; i < 8; i++) {
    byte nextByte = (byte) ((val >> i * 8) & 0xff);
    byteArr[i] = nextByte;
return byteArr;
byte[]longToBytes(long val)
Convert long value to byte array.
byte[] b = new byte[8];
for (int i = 7; i > 0; i--) {
    b[i] = (byte) val;
    val >>>= 8;
b[0] = (byte) val;
return b;
byte[]longToBytes(long value)
long To Bytes
return new byte[] { (byte) (value & 0xFF), (byte) ((value >> 8) & 0xFF), (byte) ((value >> 16) & 0xFF),
        (byte) ((value >> 24) & 0xFF), (byte) ((value >> 32) & 0xFF), (byte) ((value >> 40) & 0xFF),
        (byte) ((value >> 48) & 0xFF), (byte) ((value >> 56) & 0xFF) };
voidlongToBytes(long value, byte[] array, int offset)
Writes a long as bytes into the provided array.
array[offset + 0] = (byte) (0xff & (value >> 56));
array[offset + 1] = (byte) (0xff & (value >> 48));
array[offset + 2] = (byte) (0xff & (value >> 40));
array[offset + 3] = (byte) (0xff & (value >> 32));
array[offset + 4] = (byte) (0xff & (value >> 24));
array[offset + 5] = (byte) (0xff & (value >> 16));
array[offset + 6] = (byte) (0xff & (value >> 8));
array[offset + 7] = (byte) (0xff & value);
...
voidlongToBytes(long value, byte[] buffer, int bufferStartPosition)
long To Bytes
int endPos = bufferStartPosition + 8;
for (int idx = bufferStartPosition; idx < endPos; idx++) {
    buffer[idx] = (byte) (value & 0xFF);
    value = value >> 8;
intlongToBytes(long value, byte[] buffer, int offset)
Writes a long into a buffer.
buffer[offset + 7] = (byte) (value & 0xff);
value = value >>> 8;
buffer[offset + 6] = (byte) (value & 0xff);
value = value >>> 8;
buffer[offset + 5] = (byte) (value & 0xff);
value = value >>> 8;
buffer[offset + 4] = (byte) (value & 0xff);
value = value >>> 8;
...
voidLongToBytes4(long l, byte abyte0[])
Long To Bytes
abyte0[3] = (byte) (int) (255L & l);
abyte0[2] = (byte) (int) ((65280L & l) >> 8);
abyte0[1] = (byte) (int) ((0xff0000L & l) >> 16);
abyte0[0] = (byte) (int) ((0xffffffffff000000L & l) >> 24);
byte[]LongToBytes8(long l)
Long To Bytes
byte abyte0[] = new byte[8];
abyte0[7] = (byte) (int) (255L & l);
abyte0[6] = (byte) (int) ((65280L & l) >> 8);
abyte0[5] = (byte) (int) ((0xff0000L & l) >> 16);
abyte0[4] = (byte) (int) ((0xff000000L & l) >> 24);
abyte0[3] = (byte) (int) ((0xff00000000L & l) >> 32);
abyte0[2] = (byte) (int) ((0xff0000000000L & l) >> 40);
abyte0[1] = (byte) (int) ((0xff000000000000L & l) >> 48);
...
byte[]LongToBytesLE(final long val)
Long To Bytes LE
byte[] buf = new byte[8];
for (int i = 0; i < 8; i++) {
    buf[i] = (byte) (val >>> (8 * i));
return buf;