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

int[]toIntArray(byte[] byteArray)
to Int Array
int times = Integer.SIZE / Byte.SIZE;
int[] doubles = new int[byteArray.length / times];
for (int i = 0; i < doubles.length; i++) {
    doubles[i] = ByteBuffer.wrap(byteArray, i * times, times).getInt();
return doubles;
int[]toIntArray(final byte[] byteArray)
to Int Array
if (byteArray.length % 2 != 0) {
    throw new IllegalArgumentException("Cannot convert an odd sized byte array into an int array.");
int[] intArray = new int[byteArray.length / 2];
for (int i = 0; i < byteArray.length; i += 2) {
    intArray[i / 2] = toInt(byteArray[i], byteArray[i + 1]);
return intArray;
...
int[]toIntArray(float[] floatArray)
to Int Array
int[] ints = new int[floatArray.length];
for (int i = 0; i < floatArray.length; i++) {
    int bits = Float.floatToIntBits(floatArray[i]);
    ints[i] = bits;
return ints;
IntBuffertoIntBuffer(int[] array)
to Int Buffer
IntBuffer buffer = ByteBuffer.allocateDirect(array.length << 2).order(ByteOrder.nativeOrder())
        .asIntBuffer();
buffer.put(array).flip();
return buffer;
inttoIntFromByteArray(byte[] byteArray)
Returns an integer value out of a byte array.
ByteBuffer bb = ByteBuffer.allocate(4);
bb.position(4 - byteArray.length);
bb.put(byteArray);
bb.position(0);
return bb.getInt();
byte[]toLittleEndian(int value)
Convert the value using the little endian coding.
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(value);
return buffer.array();
ByteBuffertoLMBCS(final String value)
to LMBCS
Charset lmbcs = Charset.forName("x-lmbcs-1");
return lmbcs.encode(value);
longtoLong(byte data[])
to Long
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put(data, 0, data.length > 8 ? 8 : data.length);
buffer.flip();
return buffer.getLong();
LongtoLong(byte[] array)
Creates a Long from a byte array.
if ((array.length * 8) < Long.SIZE)
    return null;
ByteBuffer buffer = ByteBuffer.wrap(array);
return buffer.getLong();
longtoLong(byte[] bytes)
Convert a long value encoded as a byte[] back into its long value.
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return buffer.getLong();