How to Convert Integer to Hex and Vice versa in C# ?

If you want to convert an integer value to a hexa decimal value in C# and hexa decimal value to integer , you can use the “X” string formatting specifiers and the int.parse method with the method over loading option.

How to Convert Integer to Hex and Vice versa in C# ?

The code snippet to demonstrate the conversion of the integer value to hexa decimal and vice versa (hexadecimal to integer) is below.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AbundantcodeConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int decimalInput = 10;
            // Convert decimal value 10 to hexadecimal
            string hexaDecimalValue = decimalInput.ToString("X");
            Console.WriteLine(hexaDecimalValue);
            // Convert the hexadecimal value to number
            int DecimalOutput = int.Parse(hexaDecimalValue, System.Globalization.NumberStyles.HexNumber);
            Console.WriteLine(DecimalOutput);
            Console.ReadLine();
        }
          
    }
    
}

Leave Your Comment

Liked this content?. Please spread the word :)