二进制到十进制转换 - 公式? [英] Binary to Decimal Conversion - Formula?

查看:164
本文介绍了二进制到十进制转换 - 公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找了一会儿,还没有得到任何东西太有用的是,我的工作子网计算器,和好,我已经使用了十进制到二进制文件,我发现这里,虽然我还没有发现一个很好的方式,以二进制转换为十进制。

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).

我已经通过阅读一些其他职位,你可以通过某种方式除以10得到的结果可知,但我并没有真正了解它,因此,如果任何人都可以在正确的方向指向我,我会很高兴

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.

任何及所有帮助是非常AP preciated家伙! :)

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

推荐答案

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

中的任何碱的一些可以被认为是作为其数字乘以它们的地方的值的总和。例如,十进制数3906可以写成:

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:

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

这个地方值仅仅是10的幂:

The place values are simply powers of ten:

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

(请记住,采取零1电源的任何数字。)

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

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

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

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.

在实践说明,最好的解决办法是马特大的;它总是preferable使用库方法,而不是滚动您自己(除非你有一个很好的理由这样做)的。

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).

这篇关于二进制到十进制转换 - 公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆