7+ C# Decimal to Hexadecimal Converters

This post contains a total of 7+ C# Decimal to Hexadecimal Converter examples with Source Codes. All the Decimal to Hexadecimal converters are made using C# Programming language.

You can use the source code of these programs for your own personal or educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Asgar Ali

Made by Asgar Ali. A simple C# Program to convert decimal number to hexadecimal. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
           int dec = 0;
    if(int.TryParse(Console.ReadLine(),out dec))
    {
     string bin = Convert.ToString(dec,2);
     string hex = Convert.ToString(dec,16);
     Console.WriteLine("{0} in binary is " + bin,dec);
    Console.WriteLine("{0} in Hexadecimal is " + hex,dec);
    }
    else Console.WriteLine("Please input a number");
        }
    }
}

2. By Martin Ferenec

Made by Martin Ferenec. When prompted enter DEC value and the recursive method will convert it to HEX and display it. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MartinFerenec
{
	class DECtoHEX_RecursiveMethod
	{
		static void Main(string[] args)
		{
			int DEC1 = Convert.ToInt32(Console.ReadLine());
			Console.WriteLine(DECtoHEX(DEC1));
		}
		
		static string DECtoHEX(int DEC1)
        {
            if (DEC1 <= 0) return String.Empty;
            var head = DECtoHEX(DEC1 / 16);
            var remainder = DEC1 % 16;
            var tail = (char)(remainder + (remainder >= 10 ? 'A' - 10 : '0'));
            return head + tail;
        }
	}
}

3. By Sameh Hany Abd Alhalim Mohammed

Made by Sameh Hany Abd Alhalim Mohammed. ( Source )

using System;
 class program 
 { 
 public static void Main()
  {
   int decimalNumber, quotient; 
   int i = 1, j, temp = 0;
    char[]
     hexadecimalNumber = new char[100];
     char temp1;
      Console.WriteLine("Enter a Decimal Number :"); 
      decimalNumber = int.Parse(Console.ReadLine());
       quotient = decimalNumber; 
       while (quotient != 0) 
       {
        temp = quotient % 16;
         if (temp < 10) temp = temp + 48;
          else temp = temp + 55; temp1 = Convert.ToChar(temp); 
          hexadecimalNumber[i++] = temp1; quotient = quotient / 16; 
          } 
          Console.Write("Equivalent HexaDecimal Number is "); 
          for (j = i - 1; j > 0; j--) Console.Write(hexadecimalNumber[j]); Console.Read(); 
            } 
          }

4. By The WolverineπŸ‡§πŸ‡¬πŸΊπŸ‡§πŸ‡¬

Made by The WolverineπŸ‡§πŸ‡¬πŸΊπŸ‡§πŸ‡¬. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Enter your number here: ");
            int decim = int.Parse(Console.ReadLine());
            string hex = String.Empty;
            int remainder = int.MaxValue;

            while(decim > 0)
            {
                remainder = decim % 16;
                decim /= 16;
                if(remainder > 9)
                {
                    hex += (char)(65 + remainder - 10);
                }
                else
                {
                    hex += (char)('0' + remainder);
                }
            }

            for(int i =hex.Length-1; i>=0; i--)
            {
                Console.Write(hex[i]);
            }
            Console.WriteLine();
		}
	}
}

5. By Layan Moyura

Made by Layan Moyura. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {   Console .Write("Enter a decimal number : ");
            int x=Convert.ToInt32(Console.ReadLine());
            Console .WriteLine (x);
            int y;
            string h=" ";
            string n=" ";
            for(int i=0;i<100;i++){
                y=x%16;
                x=x/16;
                if(y==0){
                  h="0";  
                }
                if(y==1){
                  h="1";  
                }  
                 if(y==2){
                  h="2";  
                }
                  if(y==3){
                  h="3";  
                }
                if(y==4){
                  h="4";  
                }
                 if(y==5){
                  h="5";  
                }
                if(y==6){
                  h="6";  
                } 
                         
                if(y==7){
                  h="7";  
                }
                if(y==8){
                  h="8";  
                }
                if(y==9){
                  h="9";  
                }
                 if(y==10){
                  h="A";  
                } 
                if(y==11){
                  h="B";  
                }
                  
                 if(y==12){
                  h="C";  
                }     
                 if(y==13){
                  h="D";  
                } 
                if(y==14){
                  h="E";  
                }
                if(y==15){
                  h="F";  
                }
                n=n+h;
                if(x==0){
                    break ;
                }
            }
            Console .Write("Heaxadecimal number is : ");
            for(int i=n.Length-1;i>=0;i--){
                Console.Write (n[i]);
            }
        }
    }
}

6. By laura

Made by laura. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
			// This is another practice exercise using a for loop to convert a decimal to hexadecimaal
			
			
            int decNum = 0;
            int dNum = 0;
            string hexNumber = string.Empty;
               

            Console.Write("Enter a decimal number:  ");
            while(!int.TryParse(Console.ReadLine(), out decNum))
            {
                Console.Write("Please enter a valid number: ");
            }
            dNum = decNum;
            hexNumber = DecToHex(dNum);

   
           Console.WriteLine($"\nThe decimal number: {decNum}");
           Console.WriteLine($"The equivalent hexadecimal number: {hexNumber}");

         
		}
		
		   static string DecToHex(int x)
           {
           
            int i = 0;
            int j = 0;
            string tempNum = string.Empty;
            string hexNum = string.Empty;
            int result = 0;

            j = x;
            for (i = j; i != 0; i = i / 16)
            {

                result = j % 16;
                tempNum += HexDecode(result);
                j = j / 16;
            }

            for (int k = tempNum.Length - 1; k >= 0; k--)
            {
                hexNum += tempNum[k];

            }
            return hexNum;
            
           }
           
           
           static string HexDecode(int y)
        {
           string hexCode = string.Empty;

            switch(y)
            {
                case 0:
                    hexCode = "0";
                    break;
                case 1:
                    hexCode = "1";
                    break;
                case 2:
                    hexCode = "2";
                    break;
                case 3:
                    hexCode = "3";
                    break;
                case 4:
                    hexCode = "4";
                    break;
                case 5:
                    hexCode = "5";
                    break;
                case 6:
                    hexCode = "6";
                    break;
                case 7:
                    hexCode = "7";
                    break;
                case 8:
                    hexCode = "8";
                    break;
                case 9:
                    hexCode = "9";
                    break;
                case 10:
                    hexCode = "A";
                    break;
                case 11:
                    hexCode = "B";
                    break;
                case 12:
                    hexCode = "C";
                    break;
                case 13:
                    hexCode = "D";
                    break;
                case 14:
                    hexCode = "E";
                    break;
                case 15:
                    hexCode = "F";
                    break;


            }

            return hexCode;
        }
	}
}

7. By Malek Alsset #3

Made by Malek Alsset. ( Source )

using System; 

class MA 
{ 

    // function to convert decimal 

    // to hexadecimal 

    static void decToHexa(int n) 

    {  

        // char array to store  

        // hexadecimal number 

        char []hexaDeciNum = new char[100]; 

      

        // counter for hexadecimal number array 

        int i = 0; 

        while(n != 0) 

        {  

            // temporary variable to  

            // store remainder 

            int temp = 0; 

          

            // storing remainder in temp 

            // variable. 

            temp = n % 16; 

          

            // check if temp < 10 

            if(temp < 10) 

            { 

                hexaDeciNum[i] = (char)(temp + 48); 

                i++; 

            } 

            else

            { 

                hexaDeciNum[i] = (char)(temp + 55); 

                i++; 

            } 

          

            n = n / 16; 

        } 

      

        // printing hexadecimal number  

        // array in reverse order 

        for(int j = i - 1; j >= 0; j--) 

            Console.Write(hexaDeciNum[j]); 

    } 

      

    public static void Main (String []args)  

    { 

        int n = 2545; 

        decToHexa(n); 

    } 
} 

8. By Lacho

Made by Lacho. Enter decimal number to convert to hexadecimal. ( Source )

using System;
using System.Collections.Generic;
using System.Collections;
namespace DECToHEX
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = int.Parse(Console.ReadLine());
            Stack<int> hex = new Stack<int>();
            while (num != 0)
            {
                hex.Push(num % 16);
                num /= 16;
            }
            foreach (var n in hex)
            {
                if(n < 10)
                {
                    Console.Write(n);
                }
                else
                {
                    if (n == 10) { Console.Write('A'); }
                    if (n == 11) { Console.Write('B'); }
                    if (n == 12) { Console.Write('C'); }
                    if (n == 13) { Console.Write('D'); }
                    if (n == 14) { Console.Write('E'); }
                    if (n == 15) { Console.Write('F'); }
                }
            }
        }
    }
}