How to convert int[] to byte[]

前端 未结 6 512
花落未央
花落未央 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:08

    Maybe use this method

    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();
    }  
    

提交回复
热议问题