Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Binary to decimal using C#
Converting binary numbers to decimal is a fundamental operation in programming. In C#, you can convert binary to decimal using manual calculation methods or built-in functions. The binary number system uses base 2, where each digit position represents a power of 2.
How Binary to Decimal Conversion Works
Binary to decimal conversion follows a simple mathematical principle. Each binary digit (bit) is multiplied by the corresponding power of 2, starting from 2? for the rightmost digit −
Using Manual Calculation Method
This approach extracts each binary digit and multiplies it by the corresponding power of 2 −
using System;
class BinaryToDecimal {
static void Main(string[] args) {
int val = 1010, myBinary, remainder;
int myDecimal = 0, baseVal = 1;
myBinary = val;
while (val > 0) {
remainder = val % 10;
myDecimal = myDecimal + remainder * baseVal;
val = val / 10;
baseVal = baseVal * 2;
}
Console.WriteLine("Binary Number: " + myBinary);
Console.WriteLine("Converted to Decimal: " + myDecimal);
}
}
The output of the above code is −
Binary Number: 1010 Converted to Decimal: 10
Using Convert.ToInt32() Method
C# provides a built-in method to convert binary strings to decimal numbers −
using System;
class BinaryConverter {
static void Main(string[] args) {
string binaryString = "1010";
int decimalValue = Convert.ToInt32(binaryString, 2);
Console.WriteLine("Binary: " + binaryString);
Console.WriteLine("Decimal: " + decimalValue);
// Multiple examples
string[] binaryNumbers = {"1100", "1111", "10101"};
foreach (string binary in binaryNumbers) {
int result = Convert.ToInt32(binary, 2);
Console.WriteLine($"Binary {binary} = Decimal {result}");
}
}
}
The output of the above code is −
Binary: 1010 Decimal: 10 Binary 1100 = Decimal 12 Binary 1111 = Decimal 15 Binary 10101 = Decimal 21
Using Custom Method with Math.Pow
This approach uses mathematical power calculation for clearer understanding −
using System;
class BinaryDecimalConverter {
static int BinaryToDecimal(string binary) {
int decimalValue = 0;
int power = 0;
// Process from right to left
for (int i = binary.Length - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimalValue += (int)Math.Pow(2, power);
}
power++;
}
return decimalValue;
}
static void Main(string[] args) {
string[] testBinaries = {"1010", "1100", "11111", "10000"};
foreach (string binary in testBinaries) {
int decimal_result = BinaryToDecimal(binary);
Console.WriteLine($"Binary {binary} converts to Decimal {decimal_result}");
}
}
}
The output of the above code is −
Binary 1010 converts to Decimal 10 Binary 1100 converts to Decimal 12 Binary 11111 converts to Decimal 31 Binary 10000 converts to Decimal 16
Comparison of Methods
| Method | Advantages | Best For |
|---|---|---|
| Manual calculation | Educational, shows the process step by step | Learning binary conversion logic |
| Convert.ToInt32() | Built-in, efficient, handles validation | Production code, quick conversion |
| Math.Pow method | Clear mathematical approach, flexible | Custom implementations, educational |
Conclusion
Converting binary to decimal in C# can be accomplished through manual calculation, built-in methods like Convert.ToInt32(), or custom mathematical approaches. The built-in method is most efficient for production code, while manual methods help understand the underlying conversion process.
