Need a bit of help here:
I was assigned a project to convert a Hex values in a file to Decimal using Java without using methods available in Java that can do this directly(we have to do it the long way).
I have converted my Hex values to Binary successfully but am having trouble converting the Binary strings to Decimal. The output is not correct; I only need help in this section so I just put the first binary value I need to convert to make things simple to understand and explain. Here is what I have so far
public class binToDec {
public static void main(String[] args) {
int decimal = 0;
String binary = "101010111100110111101111101010111100";
for (int pow = (binary.length()-1); pow > -1; pow--) {
if (binary.charAt(pow)=='1'){
decimal += (Math.pow(2, pow));
}
}
System.out.print(decimal);
}
}
run: 2147483647 //this is incorrect. it should be 46118402748
Thank you so much for your help
BinToDecand notbinToDec