Java Integer Array Convert To intsToBytes(int[] data)

Here you can find the source of intsToBytes(int[] data)

Description

Convert an array of integers into a new array of bytes.

License

Open Source License

Parameter

Parameter Description
data an integer array of arbitrary size

Return

a byte array

Declaration

public static byte[] intsToBytes(int[] data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  w ww. j a v  a 2  s .  co  m*/
     * Convert an array of integers into a new array of bytes. The order of 4
     * bytes each is reversed after splitting the integers into bytes.
     * 
     * @param data
     *            an integer array of arbitrary size
     * @return a byte array
     */
    public static byte[] intsToBytes(int[] data) {
        byte[] b = new byte[data.length * 4];

        for (int i = 0; i < data.length; i++) {
            b[i * 4] = (byte) (data[i] & 0xFF);
            b[i * 4 + 1] = (byte) ((data[i] >> 8) & 0xFF);
            b[i * 4 + 2] = (byte) ((data[i] >> 16) & 0xFF);
            b[i * 4 + 3] = (byte) ((data[i] >> 24) & 0xFF);
        }

        return b;
    }
}

Related

  1. intArrayToFloatArray(int[] intArray)
  2. intArrayToIp(final int[] array, final int offset)
  3. intsToByteHighAndLow(int highValue, int lowValue)
  4. intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)
  5. intsToBytes(int[] a, int aoffset, int len, byte[] b, int boffset)
  6. intsToBytes(int[] intArray)
  7. intsToBytes(int[][] ints)
  8. intsToCommaSeparatedString(int[] ints)
  9. intsToHex(int[] val)