Help with binary to decimal
Go to solution
Solved by Guest,
It is java, there is a tag at the top.
If I out in 101 it does 5 correctly, but if I put in 110001 (49) It will return 35 which is 100011. I belive this is the cause for my other problem which is it ignores 0's at the begining or end, making it return 1 if I put in 100.
Ok. How's this:
public String binaryToDecimal(int binary) { result = 0; String binaryString = String.valueOf(binary); for (int i = 0; i < binaryString.length(); i++) { if (binaryString.charAt(i) == '1') { result += pow(2, (binaryString.length() - 1) - i); } } return Integer.toString(result);}
I think the problem is that you are going through the string from left to right, but the least significant bit is on the right.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now