Java – How to convert int[] to byte[]


I have an array of integers which represent a RGB image and would like to convert it to a byte array and save it to a file.

What's the best way to convert an array of integers to the array of bytes in Java?

Best Solution

As Brian says, you need to work out how what sort of conversion you need.

Do you want to save it as a "normal" image file (jpg, png etc)?

If so, you should probably use the Java Image I/O API.

If you want to save it in a "raw" format, the order in which to write the bytes must be specified, and then use an IntBuffer and NIO.

As an example of using a ByteBuffer/IntBuffer combination:

import java.nio.*;

class Test
    public static void main(String [] args)
        throws Exception // Just for simplicity!
        int[] data = { 100, 200, 300, 400 };

        ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);        
        IntBuffer intBuffer = byteBuffer.asIntBuffer();

        byte[] array = byteBuffer.array();

        for (int i=0; i < array.length; i++)
            System.out.println(i + ": " + array[i]);