简体   繁体   English

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

[英]Binary to Decimal Conversion - Formula?

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). 注意:记住它的FROM二进制到十进制,无论如何,我需要公式或类似的东西(意思是计算它,而不是自动化的)。

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! 任何和所有的帮助是非常感谢的家伙! :) :)

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.) (请记住,任何以0为幂的数字是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; 实际上,最好的解决方案是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). 总是更喜欢使用库方法而不是自己滚动(除非你有充分的理由这样做)。

You can do it like this: 你可以这样做:

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

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);
    }
}

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

The answer is pretty straightforward. 答案很简单。 Let's assume you have x as 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_n we 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)));
//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));
}

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

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