Represent Int32 as a Binary String in C#


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

Updated on: 22-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements