convert byte array to Integer Array - Android File Input Output

Android examples for File Input Output:Byte Array Convert

Description

convert byte array to Integer Array

Demo Code

/**/*from w w w  .j a v  a2s . c o 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
 */
//package com.book2s;

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 Tutorials