Converts a 4x4 2D byte array to a concatenated linear byte array - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Converts a 4x4 2D byte array to a concatenated linear byte array

Demo Code


//package com.java2s;

public class Main {
    /**/*from   w ww . j a v  a 2 s  . c o m*/
     * Converts a 4x4 2D byte array to a cocatenated linear byte array
     * As defined in FIPS197
     * 
     * Idea of linearising/squaring arrays taken from watne.seis720.project.AES_Utilities
     * Watne uses array indexes and some unpleasant looking maths, so I've rewritten it
     * to use arraycopy instead, which is much more efficient.
     * 
     * @param array - 4x4 2D byte array
     * @return array - linearised byte array
     */
    public static byte[] array4xToArray(byte[][] array) {
        byte[] array1x = new byte[16];

        for (int i = 0; i < 4; i++) {
            System.arraycopy(array[i], 0, array1x, (i * 4), 4);
        }

        return array1x;
    }
}

Related Tutorials