Table of Contents
Introduction
In this article, we’ll delve into the details of converting a hexadecimal number to its decimal equivalent.
Understanding Hexadecimal and Decimal
Before we jump into the writing C# program to convert Hexadecimal to Decimal, let’s briefly understand what hexadecimal and decimal systems are.
Hexadecimal System
Hexadecimal, often referred to as hex, is a base-16 number system. It uses 16 symbols (0-9 and A-F, where A stands for 10, B for 11, and so on).
For example, the hexadecimal number “1A” is equivalent to 26 in decimal.
Decimal System
Decimal is the standard base-10 number system we use every day. It consists of 10 digits: 0-9. Each digit’s place value is a power of 10.
For example, the decimal number 123
is calculated as (1 * 10^2) + (2 * 10^1) + (3 * 10^0)
.
C# Program to Convert Hexadecimal to Decimal
Now, let’s move on to the practical example of writing a C# program to convert a hexadecimal number to its decimal equivalent.
using System;
class HexToDecimalConverter
{
static void Main()
{
// Input: Hexadecimal number as a string
Console.Write("Enter a Hexadecimal number: ");
string hexInput = Console.ReadLine();
// Convert hex to decimal
int decimalResult = HexToDecimal(hexInput);
// Output: Display the result
Console.WriteLine($"Decimal equivalent: {decimalResult}");
}
static int HexToDecimal(string hex)
{
// Ensure the input is not null or empty
if (string.IsNullOrEmpty(hex))
{
throw new ArgumentException("Hexadecimal input cannot be null or empty.");
}
// Convert hex to decimal using built-in method
int decimalResult = Convert.ToInt32(hex, 16);
return decimalResult;
}
}
Output:
Enter a Hexadecimal number: 1A
Decimal equivalent: 26
Code Explanation:
- We have created the above program, which takes a hexadecimal input from the user.
- The
HexToDecimal
method converts the hex input to decimal usingConvert.ToInt32
method with base 16. - After converting the Hexadecimal to a Decimal number, the result is displayed to the user.
Let’s break down the hexadecimal number “1A” and understand how it is equivalent to 26 in decimal.
Hexadecimal Number: “1A”
In the hexadecimal system, each digit represents a power of 16, and the digits can range from 0 to 15. The letters A to F represent the values 10 to 15, respectively.
Breaking down “1A”:
- The rightmost digit ‘A’ represents the decimal value 10.
- The leftmost digit ‘1’ represents the decimal value 1.
Now, let’s calculate the decimal equivalent:
(1×16 1) + (10×160)
= (1×16) + (10×1)
= 16+10
= 26
Therefore, the hexadecimal number “1A” is equivalent to 26 in decimal. In summary, the ‘1’ contributes 16 to the total, and the ‘A’ contributes 10, resulting in a total of 26 in the decimal system.
Example 2:
Here is a C# Program to Convert Hexadecimal to Decimal without using any built-in function.
using System;
class HexToDecimalConverter
{
static void Main()
{
// Hexadecimal number to be converted
string hexNumber = "1A4";
// Calling the conversion method
int decimalNumber = ConvertHexToDecimal(hexNumber);
// Displaying the result
Console.WriteLine($"Hexadecimal: {hexNumber} \nDecimal: {decimalNumber}");
}
// Method to convert Hexadecimal to Decimal
static int ConvertHexToDecimal(string hexNumber)
{
int decimalNumber = 0;
// Loop through each character in reverse order
for (int i = hexNumber.Length - 1, power = 0; i >= 0; i--, power++)
{
char hexDigit = hexNumber[i];
// Convert hex digit to decimal
int decimalValue = HexDigitToDecimal(hexDigit);
// Add the decimal value to the result
decimalNumber += decimalValue * (int)Math.Pow(16, power);
}
return decimalNumber;
}
// Helper method to convert a single hex digit to decimal
static int HexDigitToDecimal(char hexDigit)
{
if (char.IsDigit(hexDigit))
{
return hexDigit - '0';
}
else
{
// Convert hex letter to decimal value
return char.ToUpper(hexDigit) - 'A' + 10;
}
}
}
Output:
Hexadecimal: ABC
Decimal: 2748
Recommended Articles:
- C# Program to Convert Decimal to Binary with Examples
- C# Program to Convert Binary to Decimal with Examples
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# Program to Print Multiplication Table of a Given Number
- C# program to count vowels and consonants in a string
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Program to print prime numbers from 1 to N
- C# Program to Check if a Given Number is Even or Odd
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Programs asked in Interviews
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024