简体   繁体   English

如何将字节数组转换为整数数组

[英]How to convert a Byte Array to an Int Array

I am reading a file by using: 我正在使用以下方式读取文件:

int len = (int)(new File(args[0]).length());
    FileInputStream fis =
        new FileInputStream(args[0]);
    byte buf[] = new byte[len];
    fis.read(buf);

As I found here . 我在这里发现。 Is it possible to convert byte array buf to an Int Array ? 是否可以将byte array buf转换为Int Array Is converting the Byte Array to Int Array will take significantly more space ? Byte Array转换为Int Array会占用更多空间吗?

Edit: my file contains millions of ints like, 编辑:我的文件包含数百万个整数,例如

100000000 200000000 ..... (written using normal int file wirte). 100000000 200000000 .....(使用普通的int文件写入)。 I read it to byte buffer. 我读到字节缓冲区。 Now I want to wrap it into IntBuffer array. 现在,我想将其包装到IntBuffer数组中。 How to do that ? 怎么做 ? I dont want to convert each byte to int. 我不想将每个字节转换为int。

You've said in the comments that you want four bytes from the input array to correspond to one integer on the output array, so that works out nicely. 您已经在注释中说过,您希望输入数组中的四个字节对应于输出数组中的一个整数,因此效果很好。

Depends on whether you expect the bytes to be in big-endian or little-endian order, but... 取决于您希望字节是大端还是小端顺序,但是...

 IntBuffer intBuf =
   ByteBuffer.wrap(byteArray)
     .order(ByteOrder.BIG_ENDIAN)
     .asIntBuffer();
 int[] array = new int[intBuf.remaining()];
 intBuf.get(array);

Done, in three lines. 完成,分为三行。

Converting every 4 bytes of a byte array into an integer array: 将字节数组的每4个字节转换为整数数组:

public int[] convert(byte buf[]) {
   int intArr[] = new int[buf.length / 4];
   int offset = 0;
   for(int i = 0; i < intArr.length; i++) {
      intArr[i] = (buf[3 + offset] & 0xFF) | ((buf[2 + offset] & 0xFF) << 8) |
                  ((buf[1 + offset] & 0xFF) << 16) | ((buf[0 + offset] & 0xFF) << 24);  
   offset += 4;
   }
   return intArr;
}

Is this ok for you? 这样可以吗

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

Solution for converting an array of bytes into an array of integers, where each set of 4 bytes represents an integer. 将字节数组转换为整数数组的解决方案,其中每4个字节的集合代表一个整数。 The byte input is byte[] srcByte . 字节输入是byte[] srcByte The int output is dstInt[] . int输出是dstInt[]

Little-endian source bytes: 小尾数源字节:

    int shiftBits;
    int byteNum = 0;
    int[] dstInt = new int[srcByte.length/4]; //you might have to hard code the array length

    //Convert array of source bytes (srcByte) into array of integers (dstInt)
    for (int intNum = 0; intNum < srcByte.length/4; ++intNum) {  //for the four integers
        dstInt[intNum] = 0;                                      //Start with the integer = 0

        for(shiftBits = 0; shiftBits < 32; shiftBits += 8) {     //Add in each data byte, lowest first
            dstInt[intNum] |= (srcByte[byteNum++] & 0xFF) << shiftBits;
        }
    }

For Big-Endian substitute this line: 对于Big-Endian替换此行:

    for(shiftBits = 24; shiftBits >= 0; shiftBits -= 8)  //Add in each data byte, highest first

Create a new int array and copy over the values, casting as needed. 创建一个新的int数组并复制值,并根据需要进行转换。

int[] arr = new int[len];

for(int i = 0; i < len; i++)
    arr[i] = (int)buf[i];

define "significantly". 定义“显着”。 in java, an int is 4 bytes, so by definition the array would be 4x the space. 在Java中,一个int是4个字节,因此根据定义,该数组将是该空间的4倍。 See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 参见: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

And during the conversion, you have to have both, so during the copy portion, you'd be using even more, if you were copying the whole array at once. 在转换期间,您必须同时拥有两者,因此在复制部分中,如果您要一次复制整个数组,则将使用更多资源。

as for the conversion, there are many related questions: 至于转换,有很多相关的问题:

Java - converting byte array of audio into integer array Java-将音频的字节数组转换为整数数组

In java: 在Java中:

  • byte = 8 bits 字节 = 8位
  • integer = 32 bits 整数 = 32位

and for conversion you could do something like: 为了进行转换,您可以执行以下操作:

byte[] byteArray = new byte[] {123, 12, 87};
int[] intArray = new int[byteArray.length];

// converting byteArray to intArray
for (int i = 0; i < byteArray.length; intArray[i] = byteArray[i++]);

System.out.println(Arrays.toString(intArray));

this would output: 这将输出:

[123, 12, 87]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM