Mohanapriya R Mohanapriya R
Updated date Jun 06, 2024
In this article, we will learn how to convert hexadecimal numbers to binary in C#.
  • 2.1k
  • 0
  • 0

Convert Hexadecimal to Binary in C#

To convert a hexadecimal number to binary in C#, we can use built-in functions to handle the conversion. The Convert.ToInt32() method is used for this conversion.

using System;

class Program
{
    static void Main(string[] args)
    {
        // Hexadecimal number
        string hexNumber = "1B27C"; 
        // Convert hexadecimal to binary
        string binaryNumber = Convert.ToString(Convert.ToInt32(hexNumber, 16), 2); 
        Console.WriteLine("Hexadecimal: " + hexNumber);
        Console.WriteLine("Binary: " + binaryNumber);
    }
}

In the above program, we first declare a hexadecimal number 1A7. Then we will use Convert.ToInt32() to convert the hexadecimal string to its decimal equivalent, using base 16 as the input format specifier. Finally, we  will convert the decimal number to binary using Convert.ToString() with base 2 as the output format specifier.

Output:

Hexadecimal: 1B27C
Binary: 11011001001111100

 

Comments (0)

There are no comments. Be the first to comment!!!