This post will discuss how to convert an integer to a 32-bit binary string in C#.

There are several ways to convert an integer to binary format in C#:

1. Using Convert.ToString() method

The recommended approach is to use the built-in method Convert.ToString for converting a signed integer value to its equivalent string representation in a specified base. The base must be one of 2, 8, 10, or 16; otherwise, an ArgumentException is thrown. This method is demonstrated below:

Download  Run Code

 
This method can be extended to convert from any base to any base in C# where the base must be 2, 8, 10, or 16.

Download  Run Code

2. Custom routine

We can also write our own custom routine to convert an unsigned integer to its equivalent string representation.

Download  Run Code

That’s all about converting an integer to Binary in C#.