Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: 2d (4x4) array insdie a 1d array. (Block cipher)

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2d (4x4) array insdie a 1d array. (Block cipher)

    Hi guys

    I am trying to create my block cipher as part of a uni project and what the block cipher consists of is putting in the characters in blocks of 4x4 arrays and to map it it has a look up. What i think i needto create is a 2d array inside each cell of a 1d array. Please see photo



    Thats what i am tryiring to create ^^ . Has anyone got any suggestions of how i can do this. I eventually need to be able to subsititue different cells from differnet 4x4 arrays


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    Alternatively, you can create a large 2d array, and then a second array that contains reference numbers to the start of each "block".

    For example:
    //Sentence
    String sentence = "The quick brown  fox jumps over the lazy dog";
    //Number of Columns needed
    int numColumns=sentence.length()/4;
    //Finish last entire block of 4 columns
    while(numColumns%4!=0)
    numColumns++;
    //2D Array for Sentence
    String[][] array = new String[4][numColumns];
    //1D Array for references
    int[] references = new int[numColumns/4];
    //Fill references
    for(int i=0;i<references.length;i++)
    references[i] = i*4;

    I think that is right. Haven't compiled it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    I can get that code to work im taking it a step at a time.

    What i have is i divde the number of chareacters by 16 to give a double number how ever i need to figure out how to make the double always round up to its whole number ( 12.4 > 13, 3.3 >4, 5.9>6). This will allow me to know how many 4x4 blocks to create then i can use the reference for loop to point to the stating coloum of each 4x4 block.. How can i round the double up ?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    Quote Originally Posted by fortune2k View Post
    I can get that code to work im taking it a step at a time.

    What i have is i divde the number of chareacters by 16 to give a double number how ever i need to figure out how to make the double always round up to its whole number ( 12.4 > 13, 3.3 >4, 5.9>6). This will allow me to know how many 4x4 blocks to create then i can use the reference for loop to point to the stating coloum of each 4x4 block.. How can i round the double up ?
    What you need to do is find out what the next number divisible by 16 from the number of characters. This can be done with the if statement I had above, were you use mod ( % ) to determine if it doesnt divide evenly.

    For example, if sentence.length()%16 == 0, the number is divisible by 16 and you just need to do sentence.length()/16. Otherwise, you want to do (sentence.length()/16)+1.

    That is the most exact way I know of.

    Other ways would be by doing double division and adding .9 to your decimal, but if you have a double like 5.01, then it will not round up.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    im a little confussed on how id implement it


    i want somethinbg like


    WHILE text is NOT EVENLY DIVDED BY 16
    {
    plaintext +=d 'a'
    }
    if text is evenly divded by 16
    {
    array [4][blockammount*4]
    }

    so the while loop will keep adding the letter a to the text string until it divides evenly if it does divide eaisly time that number by 4 to give the number of coloums needed in the array

    i have never used mod before so i am a little stuck

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    //Sentence
    		String sentence = "The quick brown fox jumps over the lazy dog";
     
    		//Declare variable for number of columns
    		int numColumns;
     
    		/* Mod the number of characters in the Sentence by 16.
    		 * If the Modding returns 0, it means it is divisible
    		 * and we do not need to round up. If the Modding returns
    		 * anything other than 0, it means it is not divisible and
    		 * we need to round up. To round up, we add 1 "block" (or 16)
    		 * then divide by 16.
    		 */
    		if(sentence.length() % 16 == 0)
    			numColumns = sentence.length()/16;
    		else
    			numColumns = (sentence.length()+16)/16;

    The above code will give you the number of columns, rounded up to the nearest block of 16.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    In Java, a multidimensional array is nothing but an array of arrays. So, a char[] is an array where each element holds a char, char[][] is an array where each element is a char[] and char[][][] is an array of char[][].

    For your requirement, you need to declare the array as
    char[][][] myArray = new char[3][4][4];
    Now each of the three elements of myArray is a two dimensional char array of size 4 X 4 -- a char[4][4]

    db

  8. #8
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    Quote Originally Posted by Darryl.Burke View Post
    In Java, a multidimensional array is nothing but an array of arrays. So, a char[] is an array where each element holds a char, char[][] is an array where each element is a char[] and char[][][] is an array of char[][].

    For your requirement, you need to declare the array as
    char[][][] myArray = new char[3][4][4];
    Now each of the three elements of myArray is a two dimensional char array of size 4 X 4 -- a char[4][4]

    db
    right at the moment i have

    int w=0;
      while(w !=hexarray.length)
      {
            for(int x=0; x< BlockArray.length; x++)
            {
                for(int y=0;y<4;y++)
                {
                    for(int z=0;z<4;z++)
                    {
     
                   System.out.print( BlockArray[x][y][z] = hexarray[w]);
     
                    }
     
                }
     
            }
        w++;
       }

    im also tying to fill;e each block coloum by coloum as shown in my pic..

    am I on the right lines to achive this ?

  9. #9
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    im pretty sure that the 3d array isnt what im after ive never used 3d arrays before im thinking it like a cube am i right ?

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    Quote Originally Posted by fortune2k View Post
    right at the moment i have

    int w=0;
      while(w !=hexarray.length)
      {
            for(int x=0; x< BlockArray.length; x++)
            {
                for(int y=0;y<4;y++)
                {
                    for(int z=0;z<4;z++)
                    {
     
                   System.out.print( BlockArray[x][y][z] = hexarray[w]);
     
                    }
     
                }
     
            }
        w++;
       }

    im also tying to fill;e each block coloum by coloum as shown in my pic..

    am I on the right lines to achive this ?
    I have a suggestion which can make things easier if you ever do a multi-dimensional array on a larger scale. Purely for the reason of making things more readable and easier to debug, I have gotten into the habit of breaking down multi-dimensional arrays when accessing indexes. While it provides to actual advantage from the computer's point of view, it makes it a hell of a lot easier to deal with as a human if you have very complex loops.

    Based on your code above, breaking down the array would look like this:
    int w=0;
    while(w !=hexarray.length)
    {
    	for(int x=0; x< BlockArray.length; x++)
    	{
    		char[][] tempArr1 = BlockArray[x];
     
     		for(int y=0;y<4;y++)
    		{
    			char[] tempArr2 = tempArr1[y];
     
    			for(int z=0;z<4;z++)
    			{
       				System.out.print(tempArr2[z] = hexarray[w]);
    			}
    		}
    	}
    	w++;
    }

    While it is unnecessary for a loop as basic as the one you have above, it can be extremely useful for very complex loops that use multi-dimensional arrays.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #11
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    it needs to be one big array because i need to substitutae a cell in 1 4x4 block with another one in a different 4x4 array

  12. #12
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    I have just took a manual aproach to print out what comes in each block i was trying to put a print statement in the loop but this seems to work
    code:
            for(int x=0; x< BlockArray.length; x++)
            {
                for(int y=0;y<4;y++)
                {
                    for(int z=0;z<4;z++)
                    {
     
                   BlockArray[x][z] [y] = hexarray[w++];
     
                    }
     
                }
     
            }
     
                System.out.println("Length: "+hexarray.length+" Hash array: ");
     
                for(int d=0;d<hexarray.length;d++)
                {
                   System.out.print(hexarray[d]+" ");
                }
     
                System.out.println("\nBlock 1: \n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[0][0][0], BlockArray[0][0][1],BlockArray[0][0][2],BlockArray[0][0][3]+"\n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[0][1][0], BlockArray[0][1][1],BlockArray[0][1][2],BlockArray[0][1][3]+"\n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[0][2][0], BlockArray[0][2][1],BlockArray[0][2][2],BlockArray[0][2][3]+"\n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[0][3][0], BlockArray[0][3][1],BlockArray[0][3][2],BlockArray[0][3][3]+"\n");
     
                System.out.println("Block 2: \n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[1][0][0], BlockArray[1][0][1],BlockArray[1][0][2],BlockArray[1][0][3]+"\n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[1][1][0], BlockArray[1][1][1],BlockArray[1][1][2],BlockArray[1][1][3]+"\n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[1][2][0], BlockArray[1][2][1],BlockArray[1][2][2],BlockArray[1][2][3]+"\n");
                System.out.printf("%4s %4s %4s %4s", BlockArray[1][3][0], BlockArray[1][3][1],BlockArray[1][3][2],BlockArray[1][3][3]+"\n");

    Output:
    Length: 32 Hash array: 
    41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 
    Block 1: 
     
      41   45   49  4d
      42   46   4a  4e
      43   47   4b  4f
      44   48   4c  50
    Block 2: 
     
      51   33   37  31
      30   34   38  32
      31   35   39  33
      32   36   30  34
     Lenghth of block array 2
    string length: 32

  13. #13
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    Quote Originally Posted by fortune2k View Post
    I have just took a manual aproach to print out what comes in each block i was trying to put a print statement in the loop but this seems to work
    Check out the API for Arrays#deepToString()
    Arrays#deepToString (Java Platform SE 6)

    db
    Last edited by Darryl.Burke; November 16th, 2010 at 11:10 PM. Reason: Grrr, all forums seem to have a problem converting links that contain square array brackets

  14. #14
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2d (4x4) array insdie a 1d array. (Block cipher)

    brilliant i got it working thanks for your help

Similar Threads

  1. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM
  2. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  3. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  4. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  5. TCP chat attaching cipher is not running
    By Koren3 in forum Java Networking
    Replies: 4
    Last Post: May 19th, 2009, 02:52 AM