L
lei
Hello! I made this program of converting binary to hexadecimal, there
are few errors which are new to me. Please check it out. Thanks!
<code>
import java.lang.*;
import java.io.*;
class BinaryDecoder{
public static void main(String args[]) throws IOException{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
System.out.print("Enter a number in binary: ");
String input = console.readLine();
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 hexadecimal=0;
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;
}
System.out.print("Hexadecimal: ");
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]);
}
}
}
</code>
are few errors which are new to me. Please check it out. Thanks!
<code>
import java.lang.*;
import java.io.*;
class BinaryDecoder{
public static void main(String args[]) throws IOException{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
System.out.print("Enter a number in binary: ");
String input = console.readLine();
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 hexadecimal=0;
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;
}
System.out.print("Hexadecimal: ");
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]);
}
}
}
</code>