Mohanapriya R Mohanapriya R
Updated date Feb 19, 2024
In this article, we will learn how to convert hexadecimal numbers to binary in C#. Includes a simple explanation, code example, and output demonstration for easy understanding.

Understanding Hexadecimal and Binary:

Before diving into the conversion process, let's briefly review hexadecimal and binary systems. In hexadecimal, the symbols range from 0 to 9, and then from A to F, representing values from 0 to 15. Binary, on the other hand, consists of only 0s and 1s, representing powers of 2.

Converting 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 particularly useful for this task. Here's a simple program demonstrating the conversion:

using System;

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


Hexadecimal: 1A7
Binary: 110100111

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

Comments (0)

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