3
\$\begingroup\$

I wanted to make sure that my code is properly converting between the two endians.

Here is the code where I read in an integer:

System.out.println("Please enter the integer:");
                    int  number = input.nextInt();

                    System.out.println("At what word address would you like to store the data (must be multiple of 4)?");
                    wordAddress = input.nextInt();

                    if(wordAddress != 0 || wordAddress != 4) {
                        wordAddress = wordAddress / 4;
                    }

                    for (int j = 0; j<2; j++){
                        memoryBlock[wordAddress][3] = (byte) (number & 0xFF);   
                        memoryBlock[wordAddress][2] = (byte) ((number >> 8) & 0xFF);   
                        memoryBlock[wordAddress][1] = (byte) ((number >> 16) & 0xFF);   
                        memoryBlock[wordAddress][0] = (byte) ((number >> 24) & 0xFF);
                    }

Here is the code where I read in a string:

System.out.println("Please enter the string:");
                    String s = input.next();
                    char[] firstArray = s.toCharArray();
                    char[] sArray = { 0, 0, 0, 0};


                    for(int i = 0; i<firstArray.length; i++){
                        sArray[i] = firstArray[i];
                    }


                    System.out.println("At what word address would you like to store the data (must be multiple of 4)?");
                    wordAddress = input.nextInt();

                    if(wordAddress != 0 || wordAddress != 4) { 
                        wordAddress = wordAddress / 4;
                    }
                    int j = 0;
                    for (int i = sArray.length-1; i>=0; i--){

                        memoryBlock[wordAddress][j] = (byte) sArray[i];
                    j++;

                    }

Conversion logic:

public static void converToLittleEndian(){

        byte [][] conversionBlock = new byte[20][4];


        int k = 3;
        for(int i = 0; i < memoryBlock.length-4; i++){

                conversionBlock[i][0] = memoryBlock[i][3];
                conversionBlock[i][1] = memoryBlock[i][2];
                conversionBlock[i][2] = memoryBlock[i][1];
                conversionBlock[i][3] = memoryBlock[i][0];  
        }

        for(int i = 16; i<20; i++){
            for(int j = 0; j<memoryBlock[j].length; j++){
                conversionBlock[i][j] = memoryBlock[i][j];
            }
        }

Here is a sample input/output:

Would you like to start with a big endian (B) or little endian (L) memory system?
l
Would you like to enter a string (S), integer (I) or convert (C) to big endian?
s
Please enter the string:
word
At what word address would you like to store the data (must be multiple of 4)?
0
Would you like to enter a string (S), integer (I) or convert (C) to big endian?
s
Please enter the string:
a
At what word address would you like to store the data (must be multiple of 4)?
4
Would you like to enter a string (S), integer (I) or convert (C) to big endian?
i
Please enter the integer:
1234
At what word address would you like to store the data (must be multiple of 4)?
8
Would you like to enter a string (S), integer (I) or convert (C) to big endian?
i
Please enter the integer:
12
At what word address would you like to store the data (must be multiple of 4)?
12
Would you like to enter a string (S), integer (I) or convert (C) to big endian?
c
Outputting little endian memory system contents: 
A0:     'd' 'r' 'o' 'w' 
A4:      00  00  00 'a' 
A8:      00  00  4  -46 
A12:     00  00  00  12 
A16:     00  00  00  00 
A20:     00  00  00  00 
A24:     00  00  00  00 
A28:     00  00  00  00 
A32:     00  00  00  00 
A36:     00  00  00  00 
A40:     00  00  00  00 
A44:     00  00  00  00 
A48:     00  00  00  00 
A52:     00  00  00  00 
A56:     00  00  00  00 
A60:     00  00  00  00 
Outputting big endian memory system contents: 
A0:     'w' 'o' 'r' 'd' 
A4:     'a'  00  00  00 
A8:      -46  4  00  00 
A12:     12  00  00  00 
A16:     00  00  00  00 
A20:     00  00  00  00 
A24:     00  00  00  00 
A28:     00  00  00  00 
A32:     00  00  00  00 
A36:     00  00  00  00 
A40:     00  00  00  00 
A44:     00  00  00  00 
A48:     00  00  00  00 
A52:     00  00  00  00 
A56:     00  00  00  00 
A60:     00  00  00  00
\$\endgroup\$
3
\$\begingroup\$

Unnecessary loop

In this code, you loop 2 times:

                for (int j = 0; j<2; j++){
                    memoryBlock[wordAddress][3] = (byte) (number & 0xFF);   
                    memoryBlock[wordAddress][2] = (byte) ((number >> 8) & 0xFF);   
                    memoryBlock[wordAddress][1] = (byte) ((number >> 16) & 0xFF);   
                    memoryBlock[wordAddress][0] = (byte) ((number >> 24) & 0xFF);
                }

I'm not sure why you do this twice because nothing is different between the first loop and the second loop.

Unnecessary if

In this if statement, the condition is always true:

               if(wordAddress != 0 || wordAddress != 4) {

Buffer overflow

Here, you overflow your buffer, sArray, if you input a string longer than 4 characters:

                char[] firstArray = s.toCharArray();
                char[] sArray = { 0, 0, 0, 0};

                for(int i = 0; i<firstArray.length; i++){
                    sArray[i] = firstArray[i];
                }

Your function converToLittleEndian() also has potential buffer overflows, and it's unclear what is happening to rows 16..19.

\$\endgroup\$
  • \$\begingroup\$ Thanks! Fortunately the buffer overflow will not happen because I have checks for that in other parts of the program. Good catch on all 3 parts. \$\endgroup\$ – FlipFlopSquid Oct 18 '15 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.