Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将int[]转换为byte[]_Java_Arrays_Type Conversion - Fatal编程技术网

Java 如何将int[]转换为byte[]

Java 如何将int[]转换为byte[],java,arrays,type-conversion,Java,Arrays,Type Conversion,我有一个代表RGB图像的整数数组,希望将其转换为字节数组并保存到文件中 在Java中,将整数数组转换为字节数组的最佳方法是什么?您需要先决定如何将1个整数转换为一组字节 最有可能(?)1整数到4字节,并使用移位(>或可能使用此方法 byte[] integersToBytes(int[] values) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new Data

我有一个代表RGB图像的整数数组,希望将其转换为字节数组并保存到文件中


在Java中,将整数数组转换为字节数组的最佳方法是什么?

您需要先决定如何将1个整数转换为一组字节

最有可能(?)1整数到4字节,并使用移位(
>
可能使用此方法

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();
}  
byte[]整数字节(int[]值)
{
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
DataOutputStream dos=新的DataOutputStream(BAS);
对于(int i=0;i
如前所述,您需要确定如何进行转换

是否将其保存为“普通”图像文件(jpg、png等)

如果是这样,您可能应该使用API

如果要以“原始”格式保存,则必须指定写入字节的顺序,然后使用
IntBuffer
和NIO

作为使用ByteBuffer/IntBuffer组合的示例:

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]);
        }
    }
}
import java.nio.*;
导入java.net。*;
课堂测试
{   
公共静态void main(字符串[]args)
抛出异常//只是为了简单!
{
int[]数据={100200300400};
ByteBuffer ByteBuffer=ByteBuffer.allocate(data.length*4);
IntBuffer IntBuffer=byteBuffer.asIntBuffer();
intBuffer.put(数据);
byte[]数组=byteBuffer.array();
for(int i=0;i
如果要保存到文件,可能需要使用FileOutputStream.write直接保存到文件中:

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

将指定的字节写入此输出流。写入的一般约定是将一个字节写入输出流。要写入的字节是参数b的八个低阶位。忽略b的24个高阶位


我创建了这段代码,它运行得很好:

    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;
    }
int-IntToByte(字节arrayDst[],int-arrayOrg[],int-maxOrg){
int i;
int-idxDst;
int maxDst;
//
maxDst=maxOrg*4;
//
if(arrayDst==null)
返回0;
if(arrayOrg==null)
返回0;
if(阵列长度<最大值)
返回0;
if(arrayOrg.length8);
idxDst++;
arrayDst[idxDst]=(字节)(arrayOrg[i]>>16);
idxDst++;
arrayDst[idxDst]=(字节)(arrayOrg[i]>>24);
idxDst++;
}
//
返回idxDst;
}
int-bytepoint(int-arrayDst[],byte-arrayOrg[],int-maxOrg){
int i;
INTV;
int-idxOrg;
int maxDst;
//
maxDst=maxOrg/4;
//
if(arrayDst==null)
返回0;
if(arrayOrg==null)
返回0;
if(阵列长度<最大值)
返回0;
if(arrayOrg.length对于(i=0;i我将使用“DataOutputStream”和“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));
    }
}
/**int[]-->字节[]*/
私有字节[]toByte(int[]数据)引发IOException{
byte[]bytes=新字节[data.length];
for(int i=0;i

真的很简单吗?

如果您提到选择将int[]转换为byte[]的原因,您可能会得到更好、更明确的答案。这会将int拆分为字节。我不认为这是需要的。他想将其保存到一个文件中供以后使用,因此这将是合适的。为什么要将
baos
封装在
dos
中?这与此答案几乎完全相同:(我看到的唯一区别是
ByteArrayOutputStream的初始值
).你能解释一下这个答案是如何添加新信息的吗?只是重构。
[-1000000, 24000, -1, 40]
[-1, -16, -67, -64, 0, 0, 93, -64, -1, -1, -1, -1, 0, 0, 0, 40]
[-1000000, 24000, -1, 40]