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
C# Program to Convert Decimal to Binary
Converting a decimal number to binary involves repeatedly dividing the decimal number by 2 and collecting the remainders. In C#, this can be accomplished using manual division or built-in methods like Convert.ToString().
The manual approach uses the division by 2 method where we divide the decimal number by 2, store the remainder, and continue until the quotient becomes 0. The binary representation is formed by reading the remainders in reverse order.
Using Manual Division Method
This method involves dividing the decimal number by 2 repeatedly and collecting remainders −
Example
using System;
class DecimalToBinaryConverter {
static void Main() {
int decVal = 34;
string binary = "";
Console.WriteLine("Decimal: {0}", decVal);
int originalValue = decVal;
while (decVal >= 1) {
binary = (decVal % 2) + binary;
decVal = decVal / 2;
}
Console.WriteLine("Binary: {0}", binary);
Console.WriteLine("Verification: {0} decimal = {1} binary", originalValue, binary);
}
}
The output of the above code is −
Decimal: 34 Binary: 100010 Verification: 34 decimal = 100010 binary
Using Built-in Convert.ToString() Method
C# provides the Convert.ToString() method with a base parameter to directly convert decimal to binary −
Example
using System;
class Program {
static void Main() {
int[] decimalNumbers = {34, 15, 255, 1024};
Console.WriteLine("Decimal to Binary Conversion:");
Console.WriteLine("-----------------------------");
foreach (int decVal in decimalNumbers) {
string binary = Convert.ToString(decVal, 2);
Console.WriteLine("Decimal {0,4} = Binary {1,10}", decVal, binary);
}
}
}
The output of the above code is −
Decimal to Binary Conversion: ----------------------------- Decimal 34 = Binary 100010 Decimal 15 = Binary 1111 Decimal 255 = Binary 11111111 Decimal 1024 = Binary 10000000000
Comparison of Methods
| Method | Advantages | Use Case |
|---|---|---|
| Manual Division | Educational, shows the algorithm clearly | Learning binary conversion process |
| Convert.ToString() | Simple, efficient, one-line solution | Production code, quick conversions |
Using Recursive Approach
Example
using System;
class RecursiveBinaryConverter {
static string DecimalToBinary(int decVal) {
if (decVal == 0) return "0";
if (decVal == 1) return "1";
return DecimalToBinary(decVal / 2) + (decVal % 2).ToString();
}
static void Main() {
int[] numbers = {0, 1, 8, 34, 100};
Console.WriteLine("Recursive Binary Conversion:");
Console.WriteLine("----------------------------");
foreach (int num in numbers) {
string binary = DecimalToBinary(num);
Console.WriteLine("{0,3} ? {1,8}", num, binary);
}
}
}
The output of the above code is −
Recursive Binary Conversion: ---------------------------- 0 ? 0 1 ? 1 8 ? 1000 34 ? 100010 100 ? 1100100
Conclusion
Converting decimal to binary in C# can be accomplished through manual division by 2, using Convert.ToString(decVal, 2), or recursion. The built-in method is most efficient for production use, while manual methods help understand the underlying conversion process.
