How to convert a Byte Array to an Int Array

后端 未结 7 1064
生来不讨喜
生来不讨喜 2020-11-28 12:44

I am reading a file by using:

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


        
相关标签:
7条回答
  • 2020-11-28 13:11

    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

    0 讨论(0)
  • 2020-11-28 13:13

    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];
    
    0 讨论(0)
  • 2020-11-28 13:16

    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.

    0 讨论(0)
  • 2020-11-28 13:20

    Solution for converting an array of bytes into an array of integers, where each set of 4 bytes represents an integer. The byte input is byte[] srcByte. The int output is 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:

        for(shiftBits = 24; shiftBits >= 0; shiftBits -= 8)  //Add in each data byte, highest first
    
    0 讨论(0)
  • 2020-11-28 13:29

    In java:

    • byte = 8 bits
    • integer = 32 bits

    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]
    
    0 讨论(0)
  • 2020-11-28 13:34

    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;
    }
    
    0 讨论(0)
提交回复
热议问题