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[]longToByteArray(long value)
Converts the given long value into a byte array.
return new byte[] { (byte) ((value >>> 56) & 0xFF), (byte) ((value >>> 48) & 0xFF),
        (byte) ((value >>> 40) & 0xFF), (byte) ((value >>> 32) & 0xFF), (byte) ((value >>> 24) & 0xFF),
        (byte) ((value >>> 16) & 0xFF), (byte) ((value >>> 8) & 0xFF), (byte) (value & 0xFF) };
byte[]longToByteArray(long value)
long To Byte Array
byte[] result = new byte[8];
result[0] = (byte) (value & 0x00000000000000FFL);
result[1] = (byte) ((value & 0x000000000000FF00L) >> 8);
result[2] = (byte) ((value & 0x0000000000FF0000L) >> 16);
result[3] = (byte) ((value & 0x00000000FF000000L) >> 24);
result[4] = (byte) ((value & 0x000000FF00000000L) >> 32);
result[5] = (byte) ((value & 0x0000FF0000000000L) >> 40);
result[6] = (byte) ((value & 0x00FF000000000000L) >> 48);
...
longlongToByteArray(long value, byte[] buffer, int offset)
Take the given long value and convert it into a byte array suitable for sending over the network.
if ((buffer.length - offset) < 8) {
    throw new RuntimeException("Not enough space in buffer to write long. Expected at "
            + "least 8-bytes, found " + (buffer.length - offset));
buffer[offset] = (byte) ((value >> 56) & 0xff);
buffer[offset + 1] = (byte) ((value >> 48) & 0xff);
buffer[offset + 2] = (byte) ((value >> 40) & 0xff);
buffer[offset + 3] = (byte) ((value >> 32) & 0xff);
...
voidlongToByteArray(long value, byte[] byteArray)
long To Byte Array
byteArray[7] = (byte) (value & 0xFF);
byteArray[6] = (byte) ((value >>> 8) & 0xFF);
byteArray[5] = (byte) ((value >>> 16) & 0xFF);
byteArray[4] = (byte) ((value >>> 24) & 0xFF);
byteArray[3] = (byte) ((value >>> 32) & 0xFF);
byteArray[2] = (byte) ((value >>> 40) & 0xFF);
byteArray[1] = (byte) ((value >>> 48) & 0xFF);
byteArray[0] = (byte) (value >>> 56);
...
byte[]longToByteArray(long value, byte[] data, int offset)
long To Byte Array
if (data == null)
    data = new byte[8];
long temp = Math.abs(value);
for (int i = 0; i < 8; i++) {
    data[offset + i] = (byte) (temp % 256);
    temp = temp / 256;
    if (i == 0 && value < 0)
        data[offset] = (byte) (~data[offset] + 1);
...
voidlongToByteArray(long value, byte[] dest)
long To Byte Array
int lastIndex = dest.length - 1;
for (int i = lastIndex; i >= 0; i--) {
    dest[i] = (byte) (value & 0xff);
    value = value >> 8;
byte[]longToByteArray(long value, int nrOfBytes)
Converts the given int value to an array of byte of given length.
byte[] result = new byte[nrOfBytes];
for (int i = 0; i < nrOfBytes && i < 8; i++) {
    result[nrOfBytes - 1 - i] = (byte) (value >>> (i * 8));
return result;
byte[]longToByteArray6(long addr)
Converts a long to 6 bytes array for mac addresses
byte[] mac = new byte[6];
for (int i = 0; i < 6; i++) {
    mac[i] = (byte) (addr >> (i * 8));
return mac;
byte[]longToByteArray6(long addr)
Converts a long number to a 6 bytes array for MAC addresses.
byte[] mac = new byte[MACAddrLengthInBytes];
int i = MACAddrLengthInBytes - 1;
do {
    mac[i] = (byte) addr;
    addr >>>= NumBitsInAByte;
    i--;
} while (i >= 0);
return mac;
...
byte[]longToByteArrayBE(long data)
Convert an long to a byte array (big endian).
return new byte[] { (byte) ((data >> 56) & 0xff), (byte) ((data >> 48) & 0xff),
        (byte) ((data >> 40) & 0xff), (byte) ((data >> 32) & 0xff), (byte) ((data >> 24) & 0xff),
        (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), };