C# 二进制到十进制转换 - 公式?

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

Binary to Decimal Conversion - Formula?

c#binarydecimal

提问by Nicholas

I've been searching a while and haven't gotten anything too useful yet, I'm working on a subnet calculator, and well I have used the decimal to binary which I found here, though, I haven't found a good way to convert binary to decimal.

我已经搜索了一段时间,但还没有得到任何有用的东西,我正在研究一个子网计算器,而且我已经使用了我在这里找到的十进制到二进制,但是,我还没有找到一个好的方法将二进制转换为十进制。

Note: Remember its FROM binary TO decimal, anyways, im in need of the formula or something like that (meaning calculating it, not the automated one).

注意:记住它从二进制到十进制,无论如何,我需要公式或类似的东西(意思是计算它,而不是自动化的)。

What I've understood by reading some other posts that you can somehow get the result by dividing by 10, but I didn't really understand it, so if anyone could point me in the right direction, I'd be glad.

我通过阅读其他一些帖子了解到,你可以通过除以 10 得到结果,但我并不真正理解它,所以如果有人能指出我正确的方向,我会很高兴。

Any and all help is very much appreciated guys! :)

任何和所有的帮助都非常感谢伙计们!:)

采纳答案by Ethan Brown

Doing it without LINQ:

在没有 LINQ 的情况下执行此操作:

var s = "101011";    // my binary "number" as a string
var dec = 0;
for( int i=0; i<s.Length; i++ ) {
  // we start with the least significant digit, and work our way to the left
  if( s[s.Length-i-1] == '0' ) continue;
  dec += (int)Math.Pow( 2, i );
}

A number in any base can be thought of as the sum of its digits multiplied by their place value. For example, the decimal number 3906 can be written as:

任何基数中的数字都可以被认为是其数字之和乘以其位值。例如,十进制数 3906 可以写成:

3*1000 + 9*100 + 0*10 + 6*1

The place values are simply powers of ten:

位值只是十的幂:

3*10^3 + 9*10^2 + 0*10^1 + 6*10^0

(Remember that any number taken to the power of zero is 1.)

(请记住,任何数字的零次幂都是 1。)

Binary works exactly the same way, except the base is 2, not 10. For example, the binary number 101011 can be written as:

二进制的工作方式完全相同,除了基数是 2,而不是 10。例如,二进制数 101011 可以写为:

1*2^5 + 0*2^4 + 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0

I hope that gives you a better understanding of binary numbers and how to convert them.

我希望这能让您更好地理解二进制数以及如何转换它们。

On a practical note, the best solution is Matt Grande's; it's always preferable to use a library method instead of rolling your own (unless you have a very good reason to do so).

实际上,最好的解决方案是 Matt Grande 的;最好使用库方法而不是滚动自己的方法(除非您有充分的理由这样做)。

回答by Matt Grande

You can do it like this:

你可以这样做:

string bin = "10010101010101";
long l = Convert.ToInt64(bin,2);

回答by as-cii

The answer is pretty straightforward. Let's assume you have xas a binary number:

答案很简单。让我们假设你有x一个二进制数:

string x = "10010101010101";

Since we know that the general formula to calculate is, starting from right, 2^index_1 + 2^index_2 + 2^index_nwe can use LINQ to do something like (not tested):

既然我们知道计算的通用公式是,从右开始,2^index_1 + 2^index_2 + 2^index_n我们可以使用 LINQ 来做类似的事情(未测试):

x.Reverse()
 .Select((element, i) => new { Index = i, Element = char.GetNumericValue(element) })
 .Where(a => a.Element != 0)
 .Aggregate(0.0, (a, b) => a + (Math.Pow(2, b.Index)));

回答by L.B

11010101 = 1*2^7+ 1*2^6+ 0*2^5+ 1*2^4+ 0*2^3+ 1*2^2+ 0*2^1+ 1*2^0

11010101 = 1*2^ 7+ 1*2^ 6+ 0*2^ 5+ 1*2^ 4+ 0*2^ 3+ 1*2^ 2+ 0*2^ 1+ 1*2^ 0

.................= 128 + 64 + 0 + 16 + 0 + 4 + 0 + 1

.................= 128 + 64 + 0 + 16 + 0 + 4 + 0 + 1

回答by Hachiko0

This works fine

这工作正常

using System;

class BinaryToDecimal
{
    static void Main()
    {
        double sum = 0;
        int n = 1111111; // binary number
        int strn = n.ToString().Length; //how many digits has my number
        for (int i = 0; i < strn; i++)
        {
            int lastDigit = n % 10; // get the last digit
            sum = sum + lastDigit * (Math.Pow(2, i));
            n = n / 10; //remove the last digit
        }
        Console.WriteLine(sum);
    }
}

回答by Mohammed Modi

//a is an integer array which has binary value 
sum = 0;
Array.Reverse(a);
for (int j = 0; j < a.Length;j++)
{
   if (a[j] == 1)
sum = sum + (Math.Pow(2, j));
}