This post will discuss how to convert a binary string in C# to an integer (and vice-versa).

1. Using Convert.ToInt32() method

The standard solution to convert the specified value to a 32-bit signed integer is using the built-in method Convert.ToInt32(). The specified value can be a binary string, unsigned integer, floating-point number, etc.

Download  Run Code

 
To convert the specified value to a 16-bit signed integer, use the Convert.ToInt16 method. Similarly, to convert the specified value to a 64-bit signed integer, use the Convert.ToInt64 method. The Convert class also offers the unsigned integer counterparts like ToUInt16(), ToUInt32(), and ToUInt64() method.

2. Using Convert.ToString() method

To do the opposite, i.e., convert an integer to a binary string, you can use the Convert.ToString() method. It converts the specified integer value to its equivalent string representation in the specified base. This method is demonstrated below:

Download  Run Code

That’s all about conversion between a binary string and integer in C#.