Java Byte Array to Int bytesToInts(byte[] data)

Here you can find the source of bytesToInts(byte[] data)

Description

Convert an array of bytes into a new array of integers.

License

Open Source License

Parameter

Parameter Description
data a byte array of arbitrary size

Return

an integer array

Declaration

public static int[] bytesToInts(byte[] data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w w w  .ja v  a 2s .  c  o m
     * Convert an array of bytes into a new array of integers. The order of 4
     * bytes each is reversed and then they are combined into a new integer. If
     * data has a length that is not a multiple of 4, the remaining bytes are
     * simply dropped.
     * 
     * @param data
     *            a byte array of arbitrary size
     * @return an integer array
     */
    public static int[] bytesToInts(byte[] data) {

        int[] a = new int[data.length / 4];

        for (int i = 0; i < a.length; i++) {
            a[i] = (data[i * 4] & 0xFF) | ((data[i * 4 + 1] & 0xFF) << 8) | ((data[i * 4 + 2] & 0xFF) << 16)
                    | ((data[i * 4 + 3] & 0xFF) << 24);
        }

        return a;
    }
}

Related

  1. bytesToInteger(byte[] b)
  2. bytesToInteger(byte[] b, int off)
  3. bytesToInteger(byte[] bytes)
  4. bytesToInts(byte[] a, int ao, int[] b, int bo, int len)
  5. bytesToInts(byte[] bytes, int shift, int[] spec)
  6. bytesToInts(byte[][] bytes)
  7. byteToInt(byte b[])
  8. byteToInt(byte[] b)
  9. byteToInt(byte[] b)