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)