Converting Binary Numbers to Decimal Numbers - Formula?

I searched for a while and have not yet received anything useful, I am working on a subnet calculator, and I used the decimal I found here, although I did not find a good way to convert binary to decimal.

Note. Remember that its binary FROM is decimal, in any case, they need a formula or something like that (which means calculation, not automatic).

What I understood by reading some other posts that you can somehow get the result by dividing them by 10, but I did not understand this, so if someone could point me in the right direction, I would be glad .

Any help really appreciated by the guys! :)

+4
source share
6 answers

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

+14
source

You can do it as follows:

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

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

+3
source

It works great

 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); } } 
+3
source

The answer is pretty simple. Suppose you have x as a binary number:

 string x = "10010101010101"; 

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

 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))); 
+1
source
 //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)); } 
0
source

Source: https://habr.com/ru/post/1401878/


All Articles