binary2dec.java 695 B

12345678910111213141516171819202122
  1. import flanagan.io.KeyboardInput;
  2. import java.math.BigInteger;
  3. import java.lang.Math;
  4. public class binary2dec{
  5. public static void main(String arg[]) {
  6. KeyboardInput kb = new KeyboardInput();
  7. String binaryNumber = kb.readLine("binary number =");
  8. BigInteger currentNumber, sum = BigInteger.ZERO;
  9. for (int i = 0; i < binaryNumber.length(); i++) {
  10. currentNumber = BigInteger.valueOf(Integer.parseInt(binaryNumber.substring(i,i + 1)));
  11. if (currentNumber.compareTo(BigInteger.ONE) == 0) {
  12. sum = sum.add(BigInteger.TWO.pow(binaryNumber.length() - i - 1));
  13. }
  14. }
  15. System.out.println(sum);
  16. }
  17. }