Android Utililty Methods Long to Byte Array Convert

List of utility methods to do Long to Byte Array Convert

Description

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

Method

byte[]long2bytesBE(long val, byte[] b, int off)
longbytes BE
b[off] = (byte) (val >>> 56);
b[off + 1] = (byte) (val >>> 48);
b[off + 2] = (byte) (val >>> 40);
b[off + 3] = (byte) (val >>> 32);
b[off + 4] = (byte) (val >>> 24);
b[off + 5] = (byte) (val >>> 16);
b[off + 6] = (byte) (val >>> 8);
b[off + 7] = (byte) val;
...
byte[]long2bytesLE(long val, byte[] b, int off)
longbytes LE
b[off] = (byte) val;
b[off + 1] = (byte) (val >>> 8);
b[off + 2] = (byte) (val >>> 16);
b[off + 3] = (byte) (val >>> 24);
b[off + 4] = (byte) (val >>> 32);
b[off + 5] = (byte) (val >>> 40);
b[off + 6] = (byte) (val >>> 48);
b[off + 7] = (byte) (val >>> 56);
...
intlongFitsIn(final long value)
Check how many bytes are required to store value.
if (value < 0) {
    return 8;
if (value < (1l << 4 * 8)) {
    if (value < (1l << 2 * 8)) {
        if (value < (1l << 1 * 8)) {
            return 1;
        return 2;
    if (value < (1l << 3 * 8)) {
        return 3;
    return 4;
if (value < (1l << 6 * 8)) {
    if (value < (1l << 5 * 8)) {
        return 5;
    return 6;
if (value < (1l << 7 * 8)) {
    return 7;
return 8;
byte[]longToByteArray(long a)
long To Byte Array
byte[] bArray = new byte[8];
for (int i = 0; i < bArray.length; i++) {
    bArray[i] = new Long(a & 0XFF).byteValue();
    a >>= 8;
return bArray;
byte[]longToByteArray(long l)
long To Byte Array
return ByteBuffer.allocate(8).putLong(l).array();
byte[]longToByteArray(long value)
long To Byte Array
return new byte[] { (byte) (value >>> 56), (byte) (value >>> 48),
        (byte) (value >>> 40), (byte) (value >>> 32),
        (byte) (value >>> 24), (byte) (value >>> 16),
        (byte) (value >>> 8), (byte) value };
byte[]longToBytes(long l)
Returns a byte array containing the bytes of the given long in big endian order.
return new byte[] { (byte) (l >> 56), (byte) (l >> 48),
        (byte) (l >> 40), (byte) (l >> 32), (byte) (l >> 24),
        (byte) (l >> 16 & 0xFF), (byte) (l >> 8 & 0xFF),
        (byte) (l & 0xFF) };
byte[]longToBytes(long x)
long To Bytes
buffer.clear();
buffer.putLong(0, x);
return buffer.array();
byte[]longs2bytesBE(long[] val)
longsbytes BE
if (val == null)
    return null;
byte[] b = new byte[val.length << 3];
for (int i = 0; i < val.length; i++)
    long2bytesBE(val[i], b, i << 3);
return b;
byte[]longs2bytesLE(long[] val)
longsbytes LE
if (val == null)
    return null;
byte[] b = new byte[val.length << 2];
for (int i = 0; i < val.length; i++)
    long2bytesLE(val[i], b, i << 2);
return b;