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)

Example

 Live Demo

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

Output

Integer: 30
Binary String: 11110
raja
Published on 11-Sep-2018 08:40:11
Advertisements