简体   繁体   English

将十进制int转换为二进制

[英]Convert decimal int into binary

The task i have is to write a method which converts any decimal number to binary, the number is given as an int and the converted (binary) number should also be returned as an int.我的任务是编写一个将任何十进制数转换为二进制数的方法,该数以 int 形式给出,转换后的(二进制)数也应以 int 形式返回。 Now i got somewhat of a solution, but i dont know how to save the converted number as an int, as I'm working with a string in my method!现在我得到了一些解决方案,但我不知道如何将转换后的数字保存为 int,因为我正在使用我的方法中的字符串! I feel like .parseInt() would be too easy, because then I could also just use . toBinaryString()我觉得.parseInt()太容易了,因为那样我也可以使用. toBinaryString() . toBinaryString()

static void intToBin (int a) {

String bin = "";

while (a > 0) {
    if ( a % 2 == 0 )
        bin = bin + "0";
    else
        bin = bin + "1";
    a /= 2;
}

for (int i = 0; i < bin.length();i ++) {
    System.out.print(bin.charAt(i));
}

First, binary representation is just that, the value of a decimal number into a binary string.首先,二进制表示就是这样,将十进制数的值转换为二进制字符串。 Or decimal is just a representation of a binary number.或者十进制只是二进制数的表示。 If you convert a binary number to an int it will be a decimal number (whether it has only 1's and 0's or not).如果将二进制数转换为 int,它将是一个十进制数(无论它是否只有 1 和 0)。

int a = 1001;  //is a decimal number  == 1,001
int b = 0b1001; //is a decimal number == 9

The above would print in decimal because that is the default output representation of the print methods.以上将以十进制打印,因为这是打印方法的默认 output 表示。 Internally, all numbers are 1's and 0's (or possibly even +5v and 0v depending on how specific you want to be).在内部,所有数字都是 1 和 0(或者甚至可能是 +5v 和 0v,具体取决于您想要的具体程度)。

If you want to convert a binary string to decimal, do it similarly.如果要将二进制字符串转换为十进制,请类似地进行。 Except instead of dividing by the two and getting the remainder, you multiply by two.除了除以二并得到余数之外,你乘以二。

int sum =0
String s = "11011";  //27
for (char c : s.toCharArray()) {
   sum = sum * 2 + (c-'0'); // multiply by 2 and add the bit.
}
System.out.println(sum);

prints印刷

27

Note that I don't need to store it as a string since decimal is the default representation (as I explained earlier).请注意,我不需要将其存储为字符串,因为十进制是默认表示形式(正如我之前解释的那样)。

This would do what you're asking for:这将满足您的要求:

public static int decimalToBinary(int a) {
    String result = "";
    while (a > 0) {
        int res = a % 2;
        a = a / 2;
        result = res + result;
    }
    return Integer.parseInt(result);
}
String text = etxtDecimal.getText().toString();
binary = Integer.toBinaryString(Integer.parseInt(text));

It is.这是。 We use toBinaryString to convert decimal to binary我们使用toBinaryString将十进制转换为二进制

Just a note, instead of needing the loop:只是一个注释,而不是需要循环:

for (int i = 0; i < bin.length();i ++) {
    System.out.print(bin.charAt(i));
}

the digits could be added in front to the result:数字可以添加到结果的前面

...
if ( a % 2 == 0 )
    bin = "0" + bin;
else
    bin = "1" + bin;
...
System.out.println(bin);

(could also just add the result of a % 2 , without the need of the if not sure if better ) (也可以只添加a % 2的结果,而不需要if not sure if better


if you want to save the binary representation as an int (bit strange, confusing).如果您想将二进制表示保存为int (有点奇怪,令人困惑)。
Example: input 5 , output 101示例:输入5 ,output 101

int bin = 0;
int mult = 1;
while (a > 0) {
    if (a % 2 == 1) {
        bin += mult;
    }
    mult *= 10;
    a /= 2;
}

not tested, just an idea, to inspire未经测试,只是一个想法,以激发灵感

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

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