I am having trouble getting this method, which converts an integer from binary to decimal, to work properly. The main problem I have found is that with binary numbers that end in 0, the last 0 is ignored by the program. For example, if I input 1010, the program would return 5 instead of 10. Below is my method for this conversion.
public int toDecimal(int inBase2){
int num = 0;
if(inBase2 < 0){
num = -1;
return num;
}
if(inBase2 == 0 && num == 0){
return num;
}else{
num = inBase2 % 10 * (int)(Math.pow(2, Math.log10(inBase2)));
return num + toDecimal(inBase2 / 10);
}
}
How would I go about fixing the program in a way that allows it to read the final 0 in the binary integer correctly?