java 从基数 10 到基数 2 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43244879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:14:05  来源:igfitidea点击:

Conversion from Base 10 to Base 2

javabinaryconverter

提问by RNY

I am trying to convert a number in base 10 to a number in base 2, but I have a problem. When I run the code, I get the base 2 number in the wrong order. For example, I input 54 and get 110110 instead of 011011, the correct value.

我正在尝试将以 10 为底的数字转换为以 2 为底的数字,但我遇到了问题。当我运行代码时,我以错误的顺序得到基数为 2 的数字。例如,我输入 54 并得到 110110 而不是 011011,这是正确的值。

import java.util.Scanner; 
public class DecimalToBinary
{
public static void main(String arg[]){   
  int quotient;
  int remainder;

  Scanner keyboard = new Scanner (System.in);
  System.out.println("Please enter a decimal number:"); 
  quotient = keyboard.nextInt();  

  do {
     remainder = quotient % 2;
     quotient = quotient / 2;

    // String x = String.valueOf(remainder);
    // System.out.print(x);
  System.out.print (remainder);

  } while (quotient != 0);
   }  
   }

回答by JHepp

Java has a built-in method to do this:

Java 有一个内置的方法来做到这一点:

Integer.toString(int?i, int?radix);

where

在哪里

  • iis your base ten number, and
  • radixis the base you want it in, in your case, 2.
  • i是你的十进制数,并且
  • radix是你想要的基础,在你的情况下,2。

It will return a string in binary.

它将以二进制形式返回一个字符串。

回答by Leal Li

Base 10 to Base 2 should Reverse output,I think you can use array,then reverse output array

Base 10 到 Base 2 应该反向输出,我想你可以使用array,然后反向输出array

 public static void main(String arg[]){
        int quotient;
        int remainder;
        List<Integer> arrayList=new ArrayList<Integer>();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter a decimal number:");
        quotient = keyboard.nextInt();

        do {
            remainder = quotient % 2;
            quotient = quotient / 2;

            // String x = String.valueOf(remainder);
            // System.out.print(x);
           // System.out.print (remainder);
            arrayList.add(remainder);

        } while (quotient != 0);
        ListIterator<Integer> li;
        for (li = arrayList.listIterator(); li.hasNext();) {// 将游标定位到列表结尾
            li.next();
        }
        for (; li.hasPrevious();) {// 逆序输出列表中的元素
            System.out.print(li.previous() + " ");
        }
    }

回答by olyjosh

Another inbuilt java function for this is

另一个内置的 java 函数是

Integer.toBinaryString(int num)

where num is the base 10 number you want to convert

其中 num 是您要转换的以 10 为基数的数字

回答by viraj nilakh

try this solution, its simple:

试试这个解决方案,它很简单:

  int quotient;
      int remainder;

      Scanner keyboard = new Scanner (System.in);
      System.out.println("Please enter a decimal number:"); 
      quotient = keyboard.nextInt();  
      Stack<Integer> s=new Stack<Integer>();
      do {
         remainder = quotient % 2;
         quotient = quotient / 2;
         s.add(remainder);
        // String x = String.valueOf(remainder);
        // System.out.print(x);
      //System.out.print (remainder);
      } while (quotient != 0);
      while(!s.isEmpty()){
          System.out.print(s.pop());

      }

回答by unjankify

I was able to fix your code

我能够修复你的代码

public class CustomDecimalToBinary {
    public static void Dec2Bin() {
        int quotient;
        int remainder;

        Scanner keyboard = new Scanner (System.in);
        System.out.println("Please enter a decimal number:");
        quotient = keyboard.nextInt();

        do {
            remainder = quotient % 2;

            // String x = String.valueOf(remainder);
            // System.out.print(x);
            System.out.print (remainder);

        } while ((quotient /= 2) != 0);
    }
}

Example output:

示例输出:

Please enter a decimal number:
54
011011

EDIT: It appears I solved the problem to get your desired output but your code produces the correct output...

编辑:看来我解决了问题以获得您想要的输出,但您的代码产生了正确的输出......