user4525741
user4525741

Reputation:

Converting binary to base 10 without math.pow( )?

I'm looking to create a simple program that will convert binary numbers to decimal numbers, without using math.pow(). Here's what I have so far, using Math.pow at the end:

import java.util.Scanner;
public class  Question1 {
  public static void main(String[] args) {
    System.out.println("Enter a binary number");
    Scanner inputKeyboard = new Scanner(System.in);
    String binaryNumber = inputKeyboard.nextLine();
    while (!checkIfBinary(binaryNumber)) {
      System.out.println("That is not a binary number.  Enter a binary number");
      binaryNumber = inputKeyboard.nextLine();
    }
    int decimalNumber = binaryToNumber(binaryNumber);
    System.out.println("Your number in base 10 is " + decimalNumber + ".");
  }

  public static boolean checkIfBinary(String input) {
    for (int i = 0; i < input.length(); i++) {
      if(input.charAt(i) != '0' && input.charAt(i) != '1') {
        return false;
      }
    }
    return true;
  }

  public static int binaryToNumber(String numberInput) {
    int total = 0;
    for (int i = 0; i < numberInput.length(); i++) {
      if (numberInput.charAt(i) == '1')  {
        total += (int) Math.pow(2, numberInput.length() - 1 - i);
      }
    }
    return total;
  }
}

I'm encountering a problem doing the exponentiation without math.pow. I know I need to use a loop, and this loop should multiply 2 by itself numberInput.length() - 1 - i times. But I'm having difficulty implementing this.

Upvotes: 2

Views: 2598

Answers (4)

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

parse your String to integer and provide it the base 2

int decimalValue = Integer.parseInt(yourStringOfBinary, 2);

but keep in mind that the max value of an Integer is 2^31-1 Which in binary is:

1111111111111111111111111111111

therefore, you will get java.lang.NumberFormatException error if you input bigger binary value than above, to solve this issue, use BigInteger,

int decimalValue = new BigInteger(yourBinaryString, 2).intValue()

Upvotes: 3

user1521213
user1521213

Reputation: 139

You can use Integer.parseInt.

Similar question has been answered here:

How to convert binary string value to decimal

Only difference is that in the answer referenced above, they are converting a String ("01101") into a decimal integer.

Also reference the Javadoc Integer.parseInt.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311518

I'd work backwards from the end of the string and just calculate the power for each character incrementally:

public static int binaryToNumber (String numberInput) {
    int currentPower = 1;
    int total = 0;

    for (int i = numberInput.length() - 1; i >= 0; i--) {
        if (numberInput.charAt(i) == '1')  {
            total += currentPower;
        }
        currentPower *= 2;
    }

    return total;
}

Upvotes: 1

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Integer let you do this by specifying the base of the entered number :

Integer.parseInt("101101101010111", 2); 

This does not use Math.pow :)

This is maybe not what you wanted, but anyway might help anyone.

Upvotes: 2

Related Questions