# Little/Big Endian conversion

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)?");

}

for (int j = 0; j<2; j++){
memoryBlock[wordAddress] = (byte) (number & 0xFF);
memoryBlock[wordAddress] = (byte) ((number >> 8) & 0xFF);
memoryBlock[wordAddress] = (byte) ((number >> 16) & 0xFF);
memoryBlock[wordAddress] = (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)?");

}
int j = 0;
for (int i = sArray.length-1; i>=0; i--){

j++;

}


Conversion logic:

public static void converToLittleEndian(){

byte [][] conversionBlock = new byte;

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

conversionBlock[i] = memoryBlock[i];
conversionBlock[i] = memoryBlock[i];
conversionBlock[i] = memoryBlock[i];
conversionBlock[i] = memoryBlock[i];
}

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


# Unnecessary loop

In this code, you loop 2 times:

                for (int j = 0; j<2; j++){
memoryBlock[wordAddress] = (byte) (number & 0xFF);
memoryBlock[wordAddress] = (byte) ((number >> 8) & 0xFF);
memoryBlock[wordAddress] = (byte) ((number >> 16) & 0xFF);
memoryBlock[wordAddress] = (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.

• 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. Oct 18 '15 at 6:43