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[]long2byteArray(final long number, final int length, final boolean swapHalfWord)
Converts a long number to a byte array Halfwords can be swapped by setting swapHalfWord=true.
byte[] ret = new byte[length];
int pos = 0;
long tmp_number = 0;
if (length % 2 != 0 || length < 2) {
    throw new UnsupportedOperationException();
tmp_number = number;
for (pos = length - 1; pos >= 0; pos--) {
...
voidlong2byteArray(long k, byte b[], int off)
longbyte Array
int j;
j = b.length;
if (b == null || j <= off) {
    return;
j -= off;
if (j > LONG_BYTES) {
    j = LONG_BYTES;
...
byte[]long2ByteArray(long l)
long Byte Array
byte[] array = new byte[8];
int i, shift;
for (i = 0, shift = 56; i < 8; i++, shift -= 8) {
    array[i] = (byte) (0xFF & (l >> shift));
return array;
byte[]long2ByteArray(long srcValue, int len)
long Byte Array
if (len != 2 && len != 4 && len != 6 && len != 8)
    throw new IllegalArgumentException("illegal length: " + len);
byte[] tempBytes = new byte[len];
for (int i = 0; i < len; i++) {
    long tempValue = srcValue;
    tempValue >>= 8 * i;
    tempBytes[i] = (byte) (tempValue & 0xFF);
return tempBytes;
voidlong2ByteLE(byte[] bytes, long value, int offset)
long Byte LE
checkLength(bytes, 8, offset);
bytes[offset + 0] = (byte) ((value & 0xff));
bytes[offset + 1] = (byte) ((value >> 8 * 1) & 0xff);
bytes[offset + 2] = (byte) ((value >> 8 * 2) & 0xff);
bytes[offset + 3] = (byte) ((value >> 8 * 3) & 0xff);
bytes[offset + 4] = (byte) ((value >> 8 * 4) & 0xff);
bytes[offset + 5] = (byte) ((value >> 8 * 5) & 0xff);
bytes[offset + 6] = (byte) ((value >> 8 * 6) & 0xff);
...
byte[]long2bytes(long i)
longbytes
byte[] bytes = new byte[8];
long2bytes(i, bytes, 0);
return bytes;
byte[]long2bytes(long i, int byteCount)
longbytes
byte[] b = new byte[8];
b[7] = (byte) (i);
i >>>= 8;
b[6] = (byte) (i);
i >>>= 8;
b[5] = (byte) (i);
i >>>= 8;
b[4] = (byte) (i);
...
byte[]long2bytes(long i, int byteCount)
longbytes
byte[] b = new byte[8];
b[7] = (byte) (i);
i >>>= 8;
b[6] = (byte) (i);
i >>>= 8;
b[5] = (byte) (i);
i >>>= 8;
b[4] = (byte) (i);
...
intlong2bytes(long l, byte[] data, int offset)
longbytes
data[offset++] = (byte) ((l >> 56) & 0xff);
data[offset++] = (byte) ((l >> 48) & 0xff);
data[offset++] = (byte) ((l >> 40) & 0xff);
data[offset++] = (byte) ((l >> 32) & 0xff);
data[offset++] = (byte) ((l >> 24) & 0xff);
data[offset++] = (byte) ((l >> 16) & 0xff);
data[offset++] = (byte) ((l >> 8) & 0xff);
data[offset] = (byte) (l & 0xff);
...
intlong2Bytes(long l, byte[] target, int offset)
returns the next index
for (int i = offset + 7; i >= offset; i--) {
    target[i] = (byte) (l & 0xFF);
    l >>= 8;
return offset + 8;