Android Byte Array to Int Convert toIntegerArray(byte[] input, int offset, int len)

Here you can find the source of toIntegerArray(byte[] input, int offset, int len)

Description

to Integer Array

License

Apache License

Declaration

public static int[] toIntegerArray(byte[] input, int offset, int len) 

Method Source Code

//package com.java2s;
/**//from   w  w w.j a  v a2 s  .co  m
 * Source obtained from crypto-gwt. Apache 2 License.
 * https://code.google.com/p/crypto-gwt/source/browse/crypto-gwt/src/main/java/com/googlecode/
 * cryptogwt/util/ByteArrayUtils.java
 */

public class Main {
    public static int[] toIntegerArray(byte[] input, int offset, int len) {
        assert len % 4 == 0 : "Must be a multiple of 4 bytes";
        int[] result = new int[len / 4];
        toIntegerArray(input, offset, len, result, 0);
        return result;
    }

    public static void toIntegerArray(byte[] input, int offset, int len,
            int[] output, int outputOffset) {
        assert len % 4 == 0 : "Must be a multiple of 4 bytes";
        int outputLen = len / 4;
        assert offset + outputLen < output.length : "Output buffer too short";
        for (int i = outputOffset; i < outputOffset + outputLen; i++) {
            output[i] = toInteger(input, offset);
            offset += 4;
        }
    }

    public static int toInteger(byte[] input) {
        return toInteger(input, 0);
    }

    private static int toInteger(byte[] input, int offset) {
        assert offset + 4 <= input.length : "Invalid length "
                + input.length;
        return ((input[offset++] & 0xff) << 24)
                | ((input[offset++] & 0xff) << 16)
                | ((input[offset++] & 0xff) << 8)
                | ((input[offset++] & 0xff));
    }
}

Related

  1. byteArrayToInt(byte[] b)
  2. bytes2int(byte[] data)
  3. byte2int(byte[] b)
  4. byteArray2Int(byte[] bs)
  5. byteArrayToInt(byte[] b)
  6. toIntegerArray(byte[] input, int offset, int len, int[] output, int outputOffset)
  7. toInt(byte[] bytes)
  8. toInteger(byte[] input)
  9. toInteger(byte[] input, int offset)