C# 将int转换为二进制的简单快捷的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1838963/
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-08-06 21:18:37  来源:igfitidea点击:

easy and fast way to convert an int to binary?

c#

提问by Max

What I am looking for is something like PHPs decbinfunction in C#. That function converts decimals to its representation as a string.

我正在寻找的是类似于decbinC# 中的PHP函数。该函数将小数转换为字符串形式。

For example, when using decbin(21)it returns 10101as result.

例如,当使用decbin(21)它时返回10101结果。

I found this functionwhich basically does what I want, but maybe there is a better / faster way?

我发现这个功能基本上可以满足我的要求,但也许有更好/更快的方法?

采纳答案by Konrad Rudolph

var result = Convert.ToString(number, 2);

– Almost the only use for the (otherwise useless) Convertclass.

– 几乎是(否则无用)Convert类的唯一用途。

回答by Rubens Farias

int toBase = 2;
string binary = Convert.ToString(21, toBase); // "10101"

回答by Guffa

Most ways will be better and faster than the function that you found. It's not a very good example on how to do the conversion.

大多数方法会比你找到的函数更好更快。关于如何进行转换,这不是一个很好的例子。

The built in method Convert.ToString(num, base)is an obvious choice, but you can easily write a replacement if you need it to work differently.

内置方法Convert.ToString(num, base)是一个显而易见的选择,但如果您需要它以不同的方式工作,您可以轻松地编写替代方法。

This is a simple method where you can specify the length of the binary number:

这是一个简单的方法,您可以在其中指定二进制数的长度:

public static string ToBin(int value, int len) {
   return (len > 1 ? ToBin(value >> 1, len - 1) : null) + "01"[value & 1];
}

It uses recursion, the first part (before the +) calls itself to create the binary representation of the number except for the last digit, and the second part takes care of the last digit.

它使用递归,第一部分(在 + 之前)调用自己创建除最后一位数字之外的数字的二进制表示,第二部分处理最后一位数字。

Example:

例子:

Console.WriteLine(ToBin(42, 8));

Output:

输出:

00101010

回答by Thinh Vu

This is my answer:

这是我的回答:

    static bool[] Dec2Bin(int value)
    {
        if (value == 0) return new[] { false };
        var n = (int)(Math.Log(value) / Math.Log(2));
        var a = new bool[n + 1];
        for (var i = n; i >= 0; i--)
        {
            n = (int)Math.Pow(2, i);
            if (n > value) continue;
            a[i] = true;
            value -= n;
        }
        Array.Reverse(a);
        return a;
    }

Using Pow instead of modulo and divide so i think it's faster way.

使用 Pow 而不是模数和除法,所以我认为这是更快的方法。

回答by Hans Ke?ing

To have the binary value in (at least) a specified number of digits, padded with zeroes:

要在(至少)指定位数的二进制值中使用零填充:

string bin = Convert.ToString(1234, 2).PadLeft(16, '0');

The Convert.ToString does the conversion to a binary string.
The PadLeft adds zeroes to fill it up to 16 digits.

Convert.ToString 将转换为二进制字符串。
PadLeft 添加零以填充最多 16 位数字。