How to convert int [] to byte []

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

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

+48
java arrays type-conversion
Jul 6 '09 at 8:47
source share
6 answers

As Brian says, you need to figure out exactly how you need to convert.

Do you want to save it as a "regular" 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, you must specify the byte order, then use IntBuffer and NIO.

As an example of using the ByteBuffer / IntBuffer combination:

 import java.nio.*; import java.net.*; 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(); intBuffer.put(data); byte[] array = byteBuffer.array(); for (int i=0; i < array.length; i++) { System.out.println(i + ": " + array[i]); } } } 
+61
Jul 06 '09 at 8:57
source share

Use of this method is possible.

 byte[] integersToBytes(int[] values) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); for(int i=0; i < values.length; ++i) { dos.writeInt(values[i]); } return baos.toByteArray(); } 
+8
Jul 6 '09 at 8:52
source share

You need to decide how you first convert 1 integer to a set of bytes.

Most likely (?) 1 is an integer up to 4 bytes and use shift operators ( >> or << ) to get each byte (see what the byte order is!). Copy to the byte array 4 times the integer array.

+3
Jul 06 '09 at 8:51
source share

if you intend to save the file, you might want to save it directly to the file using FileOutputStream.write:

  OutputStream os = new FileOutputStream("aa"); int[] rgb = { 0xff, 0xff, 0xff }; for (int c : rgb) { os.write(c); } os.close(); 

since he:

Writes the specified byte to this output stream. A common write contract is that one byte is written to the output stream. The byte to be written is the eight least significant bits of argument b. The high 24 bits of b are ignored.

+2
Jul 6 '09 at 9:01
source share

I created this code and it works very well:

  int IntToByte(byte arrayDst[], int arrayOrg[], int maxOrg){ int i; int idxDst; int maxDst; // maxDst = maxOrg*4; // if (arrayDst==null) return 0; if (arrayOrg==null) return 0; if (arrayDst.length < maxDst) return 0; if (arrayOrg.length < maxOrg) return 0; // idxDst = 0; for (i=0; i<maxOrg; i++){ // Copia o int, byte a byte. arrayDst[idxDst] = (byte)(arrayOrg[i]); idxDst++; arrayDst[idxDst] = (byte)(arrayOrg[i] >> 8); idxDst++; arrayDst[idxDst] = (byte)(arrayOrg[i] >> 16); idxDst++; arrayDst[idxDst] = (byte)(arrayOrg[i] >> 24); idxDst++; } // return idxDst; } int ByteToInt(int arrayDst[], byte arrayOrg[], int maxOrg){ int i; int v; int idxOrg; int maxDst; // maxDst = maxOrg/4; // if (arrayDst==null) return 0; if (arrayOrg==null) return 0; if (arrayDst.length < maxDst) return 0; if (arrayOrg.length < maxOrg) return 0; // idxOrg = 0; for (i=0; i<maxDst; i++){ arrayDst[i] = 0; // v = 0x000000FF & arrayOrg[idxOrg]; arrayDst[i] = arrayDst[i] | v; idxOrg++; // v = 0x000000FF & arrayOrg[idxOrg]; arrayDst[i] = arrayDst[i] | (v << 8); idxOrg++; // v = 0x000000FF & arrayOrg[idxOrg]; arrayDst[i] = arrayDst[i] | (v << 16); idxOrg++; // v = 0x000000FF & arrayOrg[idxOrg]; arrayDst[i] = arrayDst[i] | (v << 24); idxOrg++; } // return maxDst; } 
+1
Oct 08 '13 at 15:30
source share

I would use "DataOutputStream" with "ByteArrayOutputStream".

 public final class Converter { private static final int BYTES_IN_INT = 4; private Converter() {} public static byte [] convert(int [] array) { if (isEmpty(array)) { return new byte[0]; } return writeInts(array); } public static int [] convert(byte [] array) { if (isEmpty(array)) { return new int[0]; } return readInts(array); } private static byte [] writeInts(int [] array) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(array.length * 4); DataOutputStream dos = new DataOutputStream(bos); for (int i = 0; i < array.length; i++) { dos.writeInt(array[i]); } return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } private static int [] readInts(byte [] array) { try { ByteArrayInputStream bis = new ByteArrayInputStream(array); DataInputStream dataInputStream = new DataInputStream(bis); int size = array.length / BYTES_IN_INT; int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = dataInputStream.readInt(); } return res; } catch (IOException e) { throw new RuntimeException(e); } } } public class ConverterTest { @Test public void convert() { final int [] array = {-1000000, 24000, -1, 40}; byte [] bytes = Converter.convert(array); int [] array2 = Converter.convert(bytes); assertTrue(ArrayUtils.equals(array, array2)); System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(bytes)); System.out.println(Arrays.toString(array2)); } } 

Print

 [-1000000, 24000, -1, 40] [-1, -16, -67, -64, 0, 0, 93, -64, -1, -1, -1, -1, 0, 0, 0, 40] [-1000000, 24000, -1, 40] 
0
Jul 31 '17 at 14:25
source share



All Articles