Doing this without 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 database can be considered as the sum of its numbers multiplied by their place value. For example, the decimal number 3906 can be written as:
3*1000 + 9*100 + 0*10 + 6*1
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.)
Binary files work in exactly the same way, with the exception of base 2, not 10. For example, 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
Hope this gives you a better idea of ββbinary numbers and how to convert them.
On a practical note, Matt Grande's is the best solution; it is always preferable to use the library method instead of rolling on your own (unless you have a very good reason for this).
source share