
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to convert Decimal to Binary using C#?
Let’s say we want to convert the number 48 to binary.
Firstly, set it and use the / and % operators and loop until the value is greater than 1 −
decVal = 48; while (decVal >= 1) { val = decVal / 2; a += (decVal % 2).ToString(); decVal = val; }
Now, display every bit of the binary as shown in the complete code −
Example
using System; using System.Collections.Generic; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { int decVal; int val; string a = ""; decVal = 48; Console.WriteLine("Decimal = {0}", decVal); while (decVal >= 1) { val = decVal / 2; a += (decVal % 2).ToString(); decVal = val; } string binValue = ""; for (int i = a.Length - 1; i >= 0; i--) { binValue = binValue + a[i]; } Console.WriteLine("Binary = {0}", binValue); Console.Read(); } } }
Output
Decimal = 48 Binary = 110000
- Related Articles
- How to Convert Binary to Decimal?
- How to Convert Decimal to Binary?
- How do we convert binary to decimal and decimal to binary?
- How to Convert Decimal to Binary Using Recursion in Python?
- How to Convert Decimal to Binary, Octal, and Hexadecimal using Python?
- How to convert Decimal to Binary in JavaScript?
- How to convert Binary to Decimal in JavaScript?
- How to convert a number from Decimal to Binary using recursion in C#?
- Convert Decimal to Binary in Java
- Swift program to convert the decimal number to binary using recursion
- Haskell Program to convert the decimal number to binary using recursion
- C# Program to Convert Binary to Decimal
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- Haskell Program to convert Decimal to Binary

Advertisements