HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Little/Big Endian conversion

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversionbiglittleendian

Problem

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> 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=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 s

Solution

Unnecessary loop

In this code, you loop 2 times:

for (int j = 0; j> 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.

Code Snippets

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);
                }
if(wordAddress != 0 || wordAddress != 4) {
char[] firstArray = s.toCharArray();
                char[] sArray = { 0, 0, 0, 0};

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

Context

StackExchange Code Review Q#107917, answer score: 3

Revisions (0)

No revisions yet.