简体   繁体   English

如何使用模数在Java中将二进制转换为十进制?

[英]How to convert binary to decimal in java using modulus?

I'm trying to write a code that will allow the user to input a number in binary (with a limit of 8 digits) using modulus. 我正在尝试编写代码,允许用户使用模数以二进制形式输入数字(限制为8位数字)。 I have this so far: 到目前为止,我有:

import java.util.Scanner; 导入java.util.Scanner; public class Main { 公共班级主要{

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a Binary Number (8 digit limit)");
    int binaryNum = input.nextInt();
    int m = 0;
    int l;
    for (int n = 0; n <= 7; n++){
        l = (2^n);
        if(binaryNum%(10^(n+1)) == (10^n)){
            m = m + l;
            binaryNum = binaryNum - (10^n);
        }
    }
    System.out.println("Decimal: " + m);
}

} }

I tried inputting the number 1001010, but whenever I did, it came out as "Decimal: 2" which is clearly wrong. 我尝试输入数字1001010,但每次输入时,它都会显示为“十进制:2”,这显然是错误的。 Can someone help? 有人可以帮忙吗?

import java.util.Scanner; 

public class Main {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a Binary Number (8 digit limit)");

    int binaryNum = input.nextInt(); // reading binary number

    int m = 0; // to store decimal number

    int l = 0; // to manage power of 2
    int d = 0; // to store intermediate result

    while (binaryNum != 0){ // looping till all digits are over

        d = binaryNum % 10; // taking the last bit of the binary number

        d *= Math.pow(2, l++); // finding 2^l 

        binaryNum /= 10; // dividing the binary number by 10 to shift one bit

        m += d; // add the result to decimal 
    }

    System.out.println("Decimal: " + m); // display result
}
}

I have edited the code so that the binaryNum is converted in to decimal properly. 我已经编辑了代码,以便将binaryNum正确地转换为十进制。 Hope this helps.!! 希望这可以帮助。!!

You can use Integer.parseInt to convert a string into an integer, if you do it using a base 2: 如果使用基数2,可以使用Integer.parseInt将字符串转换为整数:

int decimalValue = Integer.parseInt(c, 2);

You should be able to get the binary value for it. 您应该能够获得它的二进制值。

Example: 例:

System.out.println(Integer.parseInt("1001010", 2));

Output: 74 输出: 74

All you need need to implement now is the restriction for the length of the string the user can, which you want to be 8, and you are good to go. 现在您需要实现的就是限制用户可以输入的字符串的长度,您希望将其设置为8,这样就可以了。

If you don't want to use the parseInt you can also do: 如果您不想使用parseInt,也可以执行以下操作:

        int binaryNumber = 10201; //example input binary
         int decimal = 0;
            int p = 0;
            while(true){
              if(binaryNumber == 0){
                break;
              } else {
                  int temp = binaryNumber%10;
                  decimal += temp*Math.pow(2, p);
                  binaryNumber = binaryNumber/10;
                  p++;
               }
            }
        System.out.println(decimal);

In this case I am converting from an int , which I guess will be ok for you, you just need to accept an int on the Scanner instead of a String , or convert the String to an int and this will do what you need, I think you misused some of the operations on your implementation. 在这种情况下,我正在从int转换,我想这对您来说还可以,您只需要在Scanner上接受int而不是String ,或者将String转换为int完成您需要的工作,我认为您滥用了实施中的某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM