Sai A Sai A
Updated date Jul 31, 2023
In this blog, we will learn how to effortlessly convert hexadecimal numbers to decimal in C#. Explore two different methods, one using a built-in function and the other employing a manual algorithm, to gain a clear understanding of the conversion process.

Introduction:

Hexadecimal and decimal are two widely used number systems in computer science and programming. Hexadecimal, also known as base-16, uses 16 symbols (0-9 and A-F) to represent numbers. Decimal, on the other hand, is the familiar base-10 system that uses 10 symbols (0-9). In this blog, we will explore two different methods for converting hexadecimal to decimal in C#.

Method 1: Using Built-in Function

The .NET framework provides a built-in method to convert hexadecimal strings to decimal using the Convert.ToInt32 method. This method takes the hexadecimal string as input and returns its decimal equivalent.

using System;

public class HexToDecimalConverter
{
    public static int ConvertHexToDecimal(string hexValue)
    {
        int decimalValue = Convert.ToInt32(hexValue, 16);
        return decimalValue;
    }
}

class Program
{
    static void Main()
    {
        string hexNumber = "1A3F";
        int decimalNumber = HexToDecimalConverter.ConvertHexToDecimal(hexNumber);
        Console.WriteLine($"Hexadecimal: {hexNumber} -> Decimal: {decimalNumber}");
    }
}

Output:

Hexadecimal: 1A3F -> Decimal: 6719

In the above method, we create a HexToDecimalConverter class that contains a static method ConvertHexToDecimal. This method utilizes the Convert.ToInt32 function, which takes two parameters: the hexadecimal string (hexValue) and the base to which the string should be converted (in this case, 16 for hexadecimal). The method then returns the resulting decimal value.

Method 2: Manual Conversion Using Algorithm

If you want to understand the underlying process of converting hexadecimal to decimal, you can implement a manual conversion algorithm. This method involves iterating through the hexadecimal digits and applying mathematical operations to obtain the decimal equivalent.

public class HexToDecimalConverter
{
    public static int ConvertHexToDecimal(string hexValue)
    {
        int decimalValue = 0;
        int power = 0;
        for (int i = hexValue.Length - 1; i >= 0; i--)
        {
            int digitValue = HexDigitToDecimalValue(hexValue[i]);
            decimalValue += digitValue * (int)Math.Pow(16, power);
            power++;
        }
        return decimalValue;
    }

    private static int HexDigitToDecimalValue(char hexDigit)
    {
        if (char.IsDigit(hexDigit))
        {
            return hexDigit - '0';
        }
        else
        {
            return char.ToUpper(hexDigit) - 'A' + 10;
        }
    }
}

class Program
{
    static void Main()
    {
        string hexNumber = "1A3F";
        int decimalNumber = HexToDecimalConverter.ConvertHexToDecimal(hexNumber);
        Console.WriteLine($"Hexadecimal: {hexNumber} -> Decimal: {decimalNumber}");
    }
}

Output:

Hexadecimal: 1A3F -> Decimal: 6719

In this manual conversion method, we create a HexToDecimalConverter class with the ConvertHexToDecimal method. The algorithm iterates through each digit of the hexadecimal string in reverse order. For each digit, we convert it to its decimal value using the HexDigitToDecimalValue method, which handles both digit and letter cases. We then multiply the decimal value of the digit by the corresponding power of 16 and accumulate the sum in decimalValue. Finally, we return the calculated decimal value.

Conclusion:

This blog explored two different methods for converting hexadecimal to decimal in C#. The first method leverages the built-in Convert.ToInt32 function, offering a quick and convenient way to perform the conversion. The second method involved a manual algorithm, providing a deeper understanding of the conversion process.

Comments (0)

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