Search code examples
javabinaryhexdecimaloctal

Converting Binary to Decimal Numbers in Java strictly using multiplication and division


I am trying to get my Binary to Decimal method to calculate properly. I have to use the multiply and/or divide methods in the "bToD" method. I can't figure out how to get it return the answer correct. It should only be returning "5", but instead it is returning 45. I need to fix this method in order to continue the remaining Hexadecimal to Binary and Octal to Binary methods.

package numType;

import java.util.Scanner;


public class  numType{

    public static String multiply2(String n) { //return 2*n
        String r = "";
        int c = 0;
        for (int i = n.length()-1; i >= 0; i--) {
            int p = (n.charAt(i)-'0')*2+c;
            c = p/10;
            r = p%10+r;
        }
        if (c > 0)
            r = c+r;
        return r;
        }
    
    public static String divide2(String n) { //return n/2
        String r = "";
        int b = 0;
        int i = 0;
        if (n.charAt(0) < '2') {
            b = 1;
            i = 1;
        }
        for (; i < n.length(); i++) {
            int p = (n.charAt(i)-'0')+b*10;
            b = p%2;
            r += p/2;
        }
        if (r.length() == 0)
            r = "0";
        return r;
    }
    
    //convert binary string b to an equivalent decimal string.
    public static String bToD(String b)
    {
        String s = "";
        int n = 0;
        if (b.charAt(b.length()-1) == '1')
            n = 1;
        else
            n = 0;
        int pow = 1;                            //INITIALIZE POWER # FROM THE SECOND TO LAST 2^1
        for (int i=b.length()-2; i>=0; i--)    // LIST #'S FROM MOST RIGHT-HAND SIDE
        {
            char ch = b.charAt(i);
            String temp = "" + ch;
            for (int j=1; j<=pow; j++) 
                temp = multiply2(temp);
            //System.out.println(temp);
            int n1 = 0;
            for (int k=0; k<temp.length(); k++)
            {
                n1 = n1*10 + (int) (temp.charAt(k)-'0');
            }
            n = n + n1;
            s = temp;
            pow++;
        }
        s = s + n;
        return s;
    }
    
    //convert decimal string d to an equivalent binary string.
    public static String dToB(String d)
    {
        String s = "";
        while (!d.equals("0"))
        {
            String d1 = divide2(d);
            d1 = multiply2(d1);
            if (d1.equals(d))
                s = "0" + s;
            else
                s = "1" + s;
            d = divide2(d);
        }
        return s;
    }
    
    //convert binary string b to an equivalent octal string.
    public static String bToO(String b) 
    {
        String s = "";
        int groups = b.length()/3;
        int index = 0;
        //System.out.println(index);                       //bToD(b)
        while (groups != index)
        {
            for (int i = b.length()-3; i >= 0; i--) 
            {
                for (int j=1; j<=i; j++)
                {
                    String temp = b.substring(b.length()-3,b.length()); //last 3 digits in binary
                    String sSub = b.substring(0,b.length()-3);          //first digits in binary
                    s = bToD(temp);
                    

                    
                }
            }
            index++;
        }
        return s;
        
    }
    
    //convert octal string o to an equivalent binary string.
    public static String oToB(String o) 
    {
        String s ="";
        int digits = o.length();
        int index = 0;
        while (digits != index)
        {
            for (int i=o.length()-1; i>=0; i--)
            {
                char ch = o.charAt(i);
                //System.out.println(digits);
                
                switch (ch)
                {
                    case '7':
                        s = s + "111";
                        index++;
                        break;
                    case '6':
                        s = s + "110";
                        index++;
                        break;
                    case '5':
                        s = s + "101";
                        index++;
                        break;
                    case '4':
                        s = s + "100";
                        index++;
                        break; 
                    case '3':
                        s = s + "011";
                        index++;
                        break;   
                    case '2':
                        s = s + "010";
                        index++;
                        break; 
                    case '1':
                        s = s + "001";
                        index++;
                        break;   
                    case '0':
                        s = s + "000";
                        index++;
                        break;    
                }
            }
        }
        return s;
    }
    
    //convert binary string b to an equivalent hexadecimal string.
    public static String bToH(String b) 
    {
        String s ="";
        return s;
    }
    
    //convert hexadecimal string h to an equivalent binary string.
    public static String hToB(String h) 
    {
        String s ="";
        return s;
    }
    
    public static void main(String[] args) {
        // TODO code application logic here
        String b,d,o,h;
        
        b = "101";
        System.out.println("Binary to Decimal:");
        System.out.println(b + " => " + bToD(b));
        
        System.out.println();
        
        System.out.println("Decimal to Binary:");
        d = "45";
        System.out.println(d + " => " + dToB(d));
        
        System.out.println();
        
        System.out.println("Binary to Octal:");
        b = "100101101";
        System.out.println(b + " => " + bToO(b));
        
        System.out.println();
        
        System.out.println("Octal to Binary:");
        o = "";
        System.out.println(o + " => " + oToB(o));
        
        System.out.println();
        
        System.out.println("Binary to Hexadecimal:");
        b = "";
        System.out.println(b + " => " + bToH(b));
        
        System.out.println();
        
        System.out.println("Hexadecimal to Binary:");
        h = "";
        System.out.println(h + " => " + hToB(h));
        
        }
    }

Solution

  • Here's a solution, yours is quite convoluted.

        //convert binary string b to an equivalent decimal string.
        public static String bToD(String b)
        {
            int ans = 0, pow = 0;
            
            //For every digit in binary
            for(int i=b.length()-1; i>=0; i--){
                // Get string of current char
                String cur = Character.toString(b.charAt(i));
                
                for (int j=0; j<pow; j++) 
                    cur = multiply2(cur);
                
                ans += Integer.parseInt(cur);
                pow++;
            }
            
            return Integer.toString(ans);
        }