在Java中严格使用乘法和除法将二进制转换为十进制数

问题描述

我试图让我的二进制到十进制方法正确计算。我必须在“bToD”方法中使用乘法和/或除法。我不知道如何让它返回正确的答案。它应该只返回“5”,而是返回 45。我需要修复这个方法,以便继续剩下的十六进制转二进制和八进制转二进制方法

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));
        
        }
    }

解决方法

这是一个解决方案,你的很复杂。

    //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);
    }