Monday, June 6, 2016

Integer (Decimal) to Binary Converter in Java

Since converting from integer to binary was such a breeze I decided to give the other way around a go. This is a little more complicated and requires a process called dividing by 2s. The algorithm is as follows:

1. Take the number and divide it by 2.
2. If there is a decimal remainder, write down a 1. If not, write down a zero.
3.Repeat step 1 using the non-decimal part of the answer to part 1.
4. When you divide and get below 1, stop.
5. Reverse the order of the 0s and 1s and that is the binary number.

Not too complicated when you see it is only 4 steps. But how to implement this? Well, we will need to read in an integer and use a loop to continuously divide by 2. But we can't store it as an integer because we are working with decimals. If we use float, we can convert it to a string and check if it contains ".5" This will mean that there is a remainder (all integers divided by 2 will either end in .0 or .5 when stored as a float.)

We will use an ArrayList to store the binary number and then print it out backwards so it is correct.

The code is below with comments inside it to help you out:
  
import java.util.ArrayList;
import java.util.Scanner;

public class BinToInt {

    public static void main(String[] args) {
        /*First create a scanner to read input then read the input*/
        Scanner read = new Scanner(System.in);  // reader for input
        System.out.println("Enter a number in integer form: "); //enter in integer form
        float n = read.nextFloat(); // scans for user input of number
       
        //We will use the divide by 2 method.
        //if we divide by 2 and there is a fractional piece left over, it is a 1
        //if it is divisible by 2, then it is a 0
        ArrayList<Integer> binNumber = new ArrayList();
        while(n >= 1) //we stop when we can't divide by 2 anymore
        {
            float divide = n / 2; //divide by two
           
            //need to convert float to string
            //need to find if a string contains a substring
            String number = String.valueOf(divide);
            String digits = number.toString();
            boolean decimal = false;
            if(digits.contains(".5")) //this is if there is a fractional component
                decimal = true;
           
            if(decimal) //if there was a decimal part
            {
                n = (float)Math.floor((double)divide); //make n the whole part of the number
                binNumber.add(1); //set it as a one
            }else{
                n = divide; //make n the whole part of the number (no decimal part anyways)
                binNumber.add(0); //make it a zero
            }
            decimal = false;
           
        }
       
        //print it out in reverse form
        for(int i = binNumber.size() - 1; i >=0; i--)
        {
            System.out.print(binNumber.get(i) + "");
        }
       

    }

}



 Voila!

No comments:

Post a Comment