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

Posted

技术标签:

【中文标题】严格使用乘法和除法在Java中将二进制数转换为十进制数【英文标题】:Converting Binary to Decimal Numbers in Java strictly using multiplication and division 【发布时间】:2021-10-06 19:17:48 【问题描述】:

我正在尝试让我的二进制到十进制方法正确计算。我必须在“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));
        
        
    

【问题讨论】:

这是作业吗?将数字(字符串)转换为 int 然后将它们转换回您想要的任何格式(二进制、十进制等)会更容易。 是的,这是一项任务 【参考方案1】:

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

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

【讨论】:

这基本上是我在评论中建议的。此答案使用Integer.parseInt() 将字符串转换为int,然后仅使用Integer.toString()int 转换为字符串。我不确定你的作业是怎么说的,但这可能不是。

以上是关于严格使用乘法和除法在Java中将二进制数转换为十进制数的主要内容,如果未能解决你的问题,请参考以下文章

仅使用O(lgβ)乘法和除法将β位整数转换为数字数组

在 Power Query 中将负文本转换为十进制数

在 C# 中将十进制数转换为双精度数会产生差异

利用倒除法将一个十进制数的正整数转换为二进制

如何在 Python 中将负指数数转换为十进制数?

数制转换