Q:

C# program to convert a hexadecimal number into a decimal number

belongs to collection: C# Basic Programs | basics

0

C# program to convert a hexadecimal number into a decimal number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to convert a hexadecimal number into a decimal number is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to convert a hexadecimal number into a decimal number.
using System;
using System.Globalization;

class ConvertDemo
{
    static int hex2dec(string hexNum)
    {
        int decNum = 0;

        decNum = int.Parse(hexNum, NumberStyles.HexNumber);

        return decNum;
    }
    static void Main()
    {
        string hexNum = "";
        int decNum    = 0;

        Console.Write("Enter a hexa-decimal number: ");
        hexNum = Console.ReadLine();

        decNum = hex2dec(hexNum);
        
        Console.WriteLine("Decimal number: " + decNum);
    }
}

Output:

Enter a hexa-decimal number: 1F
Decimal number: 31
Press any key to continue . . .

Explanation:

In the above program, we created a class ConvertDemo that contains two static methods hex2dec() and Main(). The hex2dec() method is used to convert a hexadecimal number into a decimal number using Parse() method of int structure. In the Main() method, we read the hexadecimal number and pass to the hex2dec() method that returns the converted decimal number then we printed the result on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to convert a decimal number into an oct... >>
<< C# program to convert a decimal number into a hexa...