Java Utililty Methods Integer Array to Byte Array

List of utility methods to do Integer Array to Byte Array

Description

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

Method

byte[]convertIntArrayToByteArray(int[] data)
Convert int array into byte array
byte[] dataByte = new byte[data.length];
for (int i = 0; i < data.length; i++) {
    if (data[i] < 128) {
        dataByte[i] = (byte) data[i];
    } else if (data[i] >= 128 && data[i] < 256) {
        dataByte[i] = (byte) (data[i] - 256);
    } else {
        dataByte[i] = 0;
...
byte[]convertIntArrayToByteArray(int[] myIntArray, int bytesPerInt)
convert Int Array To Byte Array
byte[] myByteArray = new byte[myIntArray.length * bytesPerInt];
for (int i = 0; i < myIntArray.length; i++) {
    byte[] myBytes = convertIntToByteArray(myIntArray[i], bytesPerInt);
    for (int j = 0; j < bytesPerInt; j++) {
        myByteArray[i * bytesPerInt + (bytesPerInt - j - 1)] = myBytes[j];
return myByteArray;
...
byte[]intArray2Bytes(int[] array)
int Array Bytes
byte[] bytes = new byte[array.length * 4];
int byteIndex = 0;
int arrayIndex = 0;
while (arrayIndex < array.length) {
    int num = array[arrayIndex++];
    bytes[byteIndex++] = (byte) ((num >> 24) & 0xff);
    bytes[byteIndex++] = (byte) ((num >> 16) & 0xff);
    bytes[byteIndex++] = (byte) ((num >> 8) & 0xff);
...
byte[]intArrayToByteArray(int[] ai)
int Array To Byte Array
byte[] ab = new byte[ai.length * 4];
for (int i = 0; i < ai.length; i++) {
    ab[i * 4] = (byte) (ai[i] >> 24);
    ab[i * 4 + 1] = (byte) (ai[i] >> 16);
    ab[i * 4 + 2] = (byte) (ai[i] >> 8);
    ab[i * 4 + 3] = (byte) ai[i];
return ab;
...
byte[]intArrayToByteArray(int[] data)
unpack an array of ints into an array of bytes
byte res[] = new byte[data.length];
for (int i = 0; i < data.length; ++i) {
    res[i] = (byte) data[i];
return res;
byte[]intArrayToByteArray(int[] ints)
Converts image int[] array (32-bit per pixel) to byte[] xRGB array (4 bytes per pixel) int<->byte[4] conversion is platform specific !
if (ints == null)
    return null;
byte[] bytes = new byte[ints.length * 4];
int intcount, bytecount;
for (intcount = 0, bytecount = 0; intcount < ints.length;) {
    bytes[bytecount++] = (byte) ((ints[intcount] & 0xFF000000) >> 24);
    bytes[bytecount++] = (byte) ((ints[intcount] & 0x00FF0000) >> 16);
    bytes[bytecount++] = (byte) ((ints[intcount] & 0x0000FF00) >> 8);
...
byte[]intArrayToBytes(int[] d)
int Array To Bytes
byte[] r = new byte[d.length * 4];
for (int i = 0; i < d.length; i++) {
    byte[] s = intToBytes(d[i]);
    for (int j = 0; j < 4; j++)
        r[4 * i + j] = s[j];
return r;