How to convert int[] to byte[]

前端 未结 6 528
花落未央
花落未央 2020-11-27 04:39

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

6条回答
  •  星月不相逢
    2020-11-27 05:05

    if your intent is to save to file you maybe want to save directly in a file using FileOutputStream.write:

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

    since it:

    Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

提交回复
热议问题