18

我正在使用以下方法读取文件:

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

正如我在这里找到的。是否可以转换byte array bufInt Array? 转换Byte ArrayInt Array会占用更多空间吗?

编辑:我的文件包含数百万个整数,例如,

100000000 200000000 .....(使用普通 int 文件写入)。我把它读到字节缓冲区。现在我想将它包装到 IntBuffer 数组中。怎么做 ?我不想将每个字节转换为 int。

4

7 回答 7

49

您在评论中说过,您希望输入数组中的四个字节对应于输出数组上的一个整数,这样效果很好。

取决于您是否希望字节是大端或小端顺序,但是......

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

完成,分三行。

于 2012-07-11T17:24:33.397 回答
6

Converting every 4 bytes of a byte array into an integer array:

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;
}
于 2012-07-11T16:36:04.000 回答
1

这适合你吗?

    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;
    }
于 2013-10-08T15:36:02.073 回答
0

Create a new int array and copy over the values, casting as needed.

int[] arr = new int[len];

for(int i = 0; i < len; i++)
    arr[i] = (int)buf[i];
于 2012-07-11T16:35:54.593 回答
0

define "significantly". in java, an int is 4 bytes, so by definition the array would be 4x the space. See: 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

于 2012-07-11T16:35:58.780 回答
0

在java中:

  • 字节= 8 位
  • 整数= 32 位

对于转换,您可以执行以下操作:

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

这将输出:

[123, 12, 87]
于 2012-07-11T16:44:43.757 回答
0

将字节数组转换为整数数组的解决方案,其中每组 4 个字节代表一个整数。字节输入是byte[] srcByte。int 输出是dstInt[].

Little-endian 源字节:

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

对于 Big-Endian 替换此行:

    for(shiftBits = 24; shiftBits >= 0; shiftBits -= 8)  //Add in each data byte, highest first
于 2018-08-02T16:14:16.897 回答