title: Decimal to Binary without using in-built functions (like parseInt, Math.pow), etc
body: <p>I need to create a program to take an input and convert it into binary output. I cannot use in-built functions such as (parseInt, Math.pow, etc.) to do this. I am stuck and need help. This is what I have so far, spread out across 2 java files.</p>
  
  <p>Driver class:</p>
  
  <pre><code>import java.util.*;
  
  public class Driver {
  
      public Driver(){
  
      }
  
      public static void main(String[] args) {
  
          Driver drive = new Driver();
  
          // scanner to read in data from the user
          Scanner scanner = new Scanner(System.in);
  
          // instance of the solver class
          Solver mySolver = new Solver();
  
          // read in the first number
          System.out.print("Enter a number to convert to binary: ");
          char [] val1 = scanner.nextLine().toCharArray();
  
          // call solver class method to obtain the number of bits
          int numBits = mySolver.howManyBits(val1);
  
          // use the number of bits and the input to get the binary representation 
          char [] binaryVal1 = mySolver.stringToBinary(val1, numBits);
  
          // print the results
          System.out.println(new String(binaryVal1));
  
          // read in the arithmetic expression
          System.out.print("Enter an arithmetic expression to convert to binary: ");
          char [] val2 = scanner.nextLine().toCharArray();
  
          // call the solver class to solve the expression
          char [] result2 = mySolver.solveExpression(val2);
  
          // print the results
          System.out.println(new String(result2));
  
          scanner.close();
      }
  
  }
  </code></pre>
  
  <p>Solver class:</p>
  
  <pre><code>import java.util.*;
  
  public class Solver{
  
      private int[] powersOf2;
  
      public Solver() {
          // Constructor method 
          int numPowersOf2 = 16;
          powersOf2 = new int[numPowersOf2];
          int exponent=1;
  
          for(int count = 1; count &lt;= numPowersOf2; count++){
              exponent *= 2;
              powersOf2[count] = exponent;
          } 
      }
  
      public static void main(String[] args) {
          // Main method for Solver class
          Solver solve = new Solver();
      }
  
      public int howManyBits(char[] val1) {
          // howManyBits method
  
          int decimal = Integer.parseInt(new String(val1));
          int logValue = (int) (Math.log(decimal)/Math.log(2));
          int numBits = 0;
  
          // Condition block to return number of bits.
  
          if (logValue &lt;= 3) {
              numBits = 4;
          }
          else if (logValue &lt;=7 &amp;&amp; logValue &gt; 3) {
              numBits = 8;
          }
          else if (logValue &lt;=15 &amp;&amp; logValue &gt; 7) {
              numBits = 16;
          }
  
          System.out.println("Bits: " + numBits);
  
          return numBits;
      }
  
      public char[] stringToBinary(char[] val1, int numBits) {
          // stringToBinary method
  
          int decimalNumber = Integer.parseInt(new String(val1));
          char[] binaryVal1 = new char[numBits];
  
          int remainder = 0;
  
          for (int count = 0; decimalNumber &gt; 0; count++) {
  
              remainder = decimalNumber % 2;  
              decimalNumber /= 2;
              char[] place = String.valueOf(remainder).toCharArray();
              binaryVal1[count] = place[count];
              System.out.println(binaryVal1[count]);
          }
  
          return binaryVal1;
      }
  
      public char[] twosComplement(char[] val1){
          return val1;
  
      }
  
      public char[] solveExpression(char[] val2) {
          // solveExpression method
          return null;
      }
  
  }
  </code></pre>