Mohanapriya R Mohanapriya R
Updated date Jun 11, 2024
In this article, we will learn how to convert binary numbers to hexadecimal using C# programming.
  • 1.4k
  • 0
  • 0

Convert Binary to Hexadecimal in C#

In binary, each digit represents a power of 2, starting from the rightmost digit. For example, the binary number 1011 represents (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0), which equals 11 in decimal. Hexadecimal, on the other hand, uses powers of 16. Each digit in a hexadecimal number represents a power of 16, starting from the rightmost digit.

Let's see how to convert binary to hexadecimal in below C# program,

using System;

class Program
{
    static void Main(string[] args)
    {
        string binaryNumber = "1011";
        int decimalNumber = Convert.ToInt32(binaryNumber, 2);
        string hexadecimalNumber = Convert.ToString(decimalNumber, 16).ToUpper();
        
        Console.WriteLine($"Binary: {binaryNumber}");
        Console.WriteLine($"Hexadecimal: {hexadecimalNumber}");
    }
}

Output:

Binary: 1011
Hexadecimal: B

Comments (0)

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