Represent Int32 as a Binary String in C#

ProgrammingServer Side ProgrammingCsharp

To represent Int632as a Binary string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 2 for Binary.

Int32 represents a 32-bit signed integer.

Firstly, set an Int64 variable −

int val = 30;

Now, convert it to a binary string by including 2 as the second parameter.

Convert.ToString(val, 2)


 Live Demo

using System;
class Demo {
   static void Main() {
      int val = 30;
      Console.WriteLine("Integer: "+val);
      Console.Write("Binary String: "+Convert.ToString(val, 2));


Integer: 30
Binary String: 11110
Updated on 22-Jun-2020 15:27:53