Background
I'm doing a college assignment, which requires me to draw a pattern represented by 4 Hexadecimal digits. So far, I've managed to create everything I need to do it, except a little tiny thing: I have a bug in my code, which renders Hexadecimal values between A - F (or Decimal 10-17) useless.
I created a method where I obtain a decimal value from an hexadecimal , and convert it to a binary value.
Here's a list of the results I've obtained:
Decimal 1 -> Binary 1 -> CORRECT
Decimal 2 -> Binary 1 -> Should be 10
Decimal 3 -> Binary 11 -> CORRECT
Decimal 4 -> Binary 1 -> Should be 100
Decimal 5 -> Binary 101 -> CORRECT
Decimal 6 -> Binary 11 -> Should be 110
Decimal 7 -> Binary 111 -> CORRECT
Decimal 8 -> Binary 1 -> Should be 1000
Decimal 9 -> Binary 1001 -> CORRECT
Decimal 10 -> Binary 101 -> Should be 1010
Decimal 11 -> Binary 1101 -> Should be 1011
Decimal 12 -> Binary 11 -> Should be 1100
Decimal 13 -> Binary 1011 -> Should be 1101
Decimal 14 -> Binary 111 -> Should be 1110
Decimal 15 -> Binary 1111 -> CORRECT
The output is actually the reverse of what it should be. How can reverse the output, or make my code output the correct order in the first place?
Edit #2
The problem here, as refered to on the original post is that:
for (int i = 0; i<=3; i++){ //loops trough my pattern array
// Will receive the binary values
while (nDecimal[i]!=0){
pattern[i]= pattern[i]*10 + nDecimal[i]%2;
nDecimal[i]/=2;
System.out.println("Binary ["+i+"] " + pattern[i]);
}
is not working correctly, and is outputting the binary digits on the inverse order. In short, here's the code that does the conversion:
int myDecimal;
int result;
while (myDecimal!=0){
result= result*10 + myDecimal%2;
myDecimal=2;
}