Rewrite a byte array as an int array (the array can be padded with zeros) - Java java.lang

Java examples for java.lang:byte Array to int

Description

Rewrite a byte array as an int array (the array can be padded with zeros)

Demo Code

/*******************************************************************************
 * Copyright (c) 2008 JCrypTool Team and Contributors
 * /* w ww  .j  av  a  2  s  .  co  m*/
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] input = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(toIntArray(input)));
    }

    /**
     * Rewrite a byte array as an int array (the array can be padded with zeros)
     * 
     * @param input - the byte array
     * @return int array
     */
    public static int[] toIntArray(byte[] input) {
        int[] result = new int[(input.length + 3) >>> 2];
        int index, shiftpos;
        for (int i = 0; i < input.length; i++) {
            index = i >>> 2;
            shiftpos = i % 4;
            result[index] ^= (input[i] & 0xff) << (8 * shiftpos);
        }
        return result;
    }
}

Related Tutorials