1 2 Previous Next 16 Replies Latest reply: Apr 24, 2013 3:08 AM by 1004917

are few errors which are new to me. Please check it out. Thanks!
import java.lang.*;
import java.io.*;

class BinaryDecoder{
public static void main(String args[]) throws IOException{
System.out.print("Enter a number in binary: ");

int decimal=0;
for(int counter=input.length()-1; counter>=0; counter--){
if(input.charAt(counter)=='1'){
int exp=input.length()-1-counter;
decimal+=Math.pow(2,exp);
}
}

int powerOfTen=1;
int number=decimal;
int counter=0;
int[] hex = new int[20];

while(number>0){
int remainder=number%16;
hex[counter] = remainder;
counter++;
number/=16;
}

for(int count=hex.length; count>=0; count--){
if(hex[count]==10)
System.out.print("A");
else if(hex[count]==11)
System.out.print("B");
else if(hex[count]==12)
System.out.print("C");
else if(hex[count]==13)
System.out.print("D");
else if(hex[count]==14)
System.out.print("E");
else if(hex[count]==15)
System.out.print("F");
else
System.out.print(hex[count]);
}
}

}
Message was edited by:
need_help88
• ###### 1. Re: Binary to Hexadecimal Conversion
Don't ask us to copy your code to disk and try to compile it for you.

What errors are you getting?
• ###### 2. Re: Binary to Hexadecimal Conversion
I made this program of converting binary representation to
there are few errors which are new to me.
for(int count=hex.length; count>=0; count--){
if(hex[count]==10)
You seem to be starting
one beyond the end of the array ...
• ###### 3. Re: Binary to Hexadecimal Conversion
Please explain further. I'm really new to java. Thank you so much!
• ###### 4. Re: Binary to Hexadecimal Conversion
Error: ArrayIndexOutOfBoundsException

//this line:
if(hex[count]==10)

can you explain this error and a possible solution. Thank you!
• ###### 5. Re: Binary to Hexadecimal Conversion
It's working now. Another problem:

Enter a number in binary: 10000011

How can I remove the zeros?
• ###### 6. Re: Binary to Hexadecimal Conversion
Error: ArrayIndexOutOfBoundsException

//this line:
if(hex[count]==10)

can you explain this error and a possible solution.
Thank you!
Read reply 2 again. The error is in the first line of code tschodt quoted.
• ###### 7. Re: Binary to Hexadecimal Conversion
It's working now. Another problem:

Enter a number in binary: 10000011

How can I remove the zeros?
String s = "00000000000000000083";
System.out.println(s.replaceAll("^0+", ""));
• ###### 8. Re: Binary to Hexadecimal Conversion
is there another way? because we are not supposed to use other methods.. Thank you! Very much appreciated!
• ###### 10. Re: Binary to Hexadecimal Conversion
It's not a string but an array of integers.
• ###### 11. Re: Binary to Hexadecimal Conversion
It's not a string but an array of integers.
Here's an idea:
class BinaryDecoder{

public static void main(String args[]) throws IOException {

int startIndex = hex.length-1;

for(int i = ... /* your code here */ ) {
// scan through your int array 'hex' from right to left
// the moment you encounter something different than 0,
// assign 'i' to 'startIndex' and exit the for-loop using the
// 'break' keyword
}

for(int count = startIndex; count >= 0; count--) {  // <-- see the variable 'startIndex' there?
if(hex[count]==10)      System.out.print("A");
else if(hex[count]==11) System.out.print("B");
else if(hex[count]==12) System.out.print("C");
else if(hex[count]==13) System.out.print("D");
else if(hex[count]==14) System.out.print("E");
else if(hex[count]==15) System.out.print("F");
else                    System.out.print(hex[count]);
}
}
}
Btw, I'm assuming this is one of your first Java applications, because you really shouldn't have cranked everything in your main-method. Try to separate some tasks in methods next time.
http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html

Good luck.
• ###### 12. Re: Binary to Hexadecimal Conversion
Thank you so much but what is something different than zero? Thank you!
• ###### 13. Re: Binary to Hexadecimal Conversion
Thank you so much but what is something different
than zero? Thank you!
Like I said:
// the moment you encounter something different than 0,
// assign 'i' to 'startIndex' and exit the for-loop using the
// 'break' keyword
Pseudo code:
for(int i = /* ... */ ) {
if(/* something is different than 0 */) {
// assign 'i' to 'startIndex'
// break out of this for-loop
}
}
• ###### 14. Re: Binary to Hexadecimal Conversion
Thank you so much but what is something different than zero?
Anything that is not equal to zero?
1 2 Previous Next