# Convert Integer to Binary in C#

This tutorial will discuss methods to convert a decimal integer to a binary string in C#.

## Convert Integer to Binary With the Conventional Method in C

The conventional method specifies that to convert a decimal number to a binary number, we have to keep dividing the number by `2` and store the remainder of each step until the number is less than `2`. We can convert a decimal integer to a binary string using this logic and concatenating the result to a string variable. See the below example.

``````using System;

namespace convert_int_to_binary
{
class Program
{
static void method1()
{
int decimalNumber = 15;
int remainder;
string binary = string.Empty;

while (decimalNumber > 0)
{
remainder = decimalNumber % 2;
decimalNumber /= 2;
binary = remainder.ToString() + binary;
}
Console.WriteLine("Binary:  {0}", binary);
}
static void Main(string[] args)
{
method1();
}
}
}
``````

Output:

``````1111
``````

We converted the decimal value `15` to a binary string containing `1111` with the conventional logic in C#.

## Convert Integer to Binary With the `Convert.ToString()` Function in C

Another simpler method for converting a decimal integer variable to a binary string variable is to use `Convert.ToString()` function in C#. The `Convert.ToString(dec, 2)` converts the `dec` to base `2` and returns the answer in the string form. The following code example shows us how to convert a decimal integer to a binary string with the `Convert.ToString()` function in C#.

``````using System;

namespace convert_int_to_binary
{
class Program
{
static void method2()
{
int value = 15;
string binary = Convert.ToString(value, 2);
Console.WriteLine("Binary:  {0}", binary);
}
static void Main(string[] args)
{
method2();
}
}
}
``````

Output:

``````1111
``````

This code is much simpler and more easy-to-understand than the previous approach. We converted the decimal value `15` to a binary string containing `1111` with the `Convert.ToString()` function in C#.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - Csharp Integer

• Convert String to Int in C#
• ## Related Article - Csharp Binary

• Convert Int to Bool in C#
• Convert Double to Int in C#