Java Utililty Methods Convert via ByteBuffer

List of utility methods to do Convert via ByteBuffer

Description

The list of methods to do Convert via ByteBuffer are organized into topic(s).

Method

byte[]intToBytes(final int value)
Convert an int into a byte array.
synchronized (intBuffer) {
    intBuffer.clear();
    intBuffer.putInt(value);
    return intBuffer.array();
byte[]intToBytes(final int x)
Converts an int value into an array of 4 bytes.
final ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(x);
return buffer.array();
byte[]intToBytes(int i, byte[] backingStore, int offset)
Convert a Java int to a 4-byte array.
ByteBuffer byteBuffer = ByteBuffer.allocate(NUM_BYTES_IN_INT);
byteBuffer.putInt(i);
return byteBuffer.array();
byte[]intToBytes(int n)
int To Bytes
byte[] bytes = new byte[Integer.SIZE / 8];
ByteBuffer b = ByteBuffer.wrap(bytes);
b.asIntBuffer().put(n);
return bytes;
byte[]intToBytes(int tagId)
int To Bytes
ByteBuffer byteBuffer = ByteBuffer.allocate(1).putInt(tagId);
byte[] bytes = byteBuffer.array();
return bytes;
byte[]intToBytesLE(int value)
int To Bytes LE
return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array();
byte[]intToBytesWithLen(int x, int len)
Write a big-endian integer into the least significant bytes of a byte array.
if (len <= 4) {
    return trailingBytes(intToBytes(x), len);
} else {
    ByteBuffer bb = ByteBuffer.allocate(len);
    bb.position(len - 4);
    bb.putInt(x);
    assert bb.remaining() == 0;
    return bb.array();
...
byte[]long2bytes(long num)
longbytes
ByteBuffer buffer = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(num);
return buffer.array();
longlongFromBytes(byte[] array)
long From Bytes
if (array.length != Long.BYTES) {
    throw new IllegalArgumentException(String.format("Invalid array lenght:%d", array.length));
ByteBuffer bb = ByteBuffer.allocate(Long.BYTES);
bb.put(array);
bb.flip();
return bb.getLong();
longlongFromBytes(byte[] value)
long From Bytes
return ByteBuffer.wrap(value).getLong();