w3resource

C#: Convert a hexadecimal number to decimal number


Hexadecimal to Decimal

Write a C# program to convert a hexadecimal number to a decimal number.

C# Sharp Exercises: Convert a hexadecimal number to decimal number

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;

public class Exercise30 {
    public static void Main() {
        // Declare a string variable 'hexval' and assign a hexadecimal value "4B0" to it
        string hexval = "4B0";

        // Display the original hexadecimal number
        Console.WriteLine("Hexadecimal number: " + hexval);

        // Convert the hexadecimal string 'hexval' to its decimal equivalent
        int decValue = int.Parse(hexval, System.Globalization.NumberStyles.HexNumber);

        // Display a message indicating conversion to different number systems
        Console.WriteLine("Convert to-");

        // Display the decimal value obtained from the hexadecimal conversion
        Console.WriteLine("Decimal number: " + decValue);
    }
}

Sample Output:

Hexadecimal number: 4B0                                                
Convert to-                                                            
Decimal number: 1200 

Flowchart:

Flowchart: C# Sharp Exercises - Convert a hexadecimal number to decimal number

For more Practice: Solve these Related Problems:

  • Write a C# program to convert a hexadecimal number to binary and octal formats as well.
  • Write a C# program to take a hex number input and identify if it is divisible by 5 after converting to decimal.
  • Write a C# program to convert hexadecimal to decimal and verify if the result is a prime number.
  • Write a C# program to evaluate a math expression where operands are hexadecimal numbers and result is in decimal.

Go to:


PREV : File Size in Bytes.
NEXT : Multiply Two Arrays.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.