This post contains a total of 8+ Hand-Picked C# Hexadecimal to Decimal Converter Examples. All the Hexadecimal to Decimal converters are made using C Sharp Programming language.
You can use the source codes 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. C# Program to convert hexadecimal value to decimal. ( 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)
{
string hex = Console.ReadLine();
if(isHex(hex))
{
Int64 val = Convert.ToInt64(hex,16);
Console.WriteLine("The value of " + hex + " in decimal is {0}",val);
}
else Console.WriteLine("Please input a valid hexadecimal value");
}
static bool isHex(string hex)
{
string x = "0123456789abcdefABCDEF";
var q =(from n in hex where ! x.Contains(n) select n).ToArray();
return (q.Length == 0);
}
}
}
2. By ChillPill ๐ถ
Made by ChillPill ๐ถ. Simple C# Converter. ( 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)
{
string hexinput = Console.ReadLine();
int decimaloutput=0;
int index= hexinput.Length;
foreach (char letter in hexinput){
index--;
decimaloutput += hextodec(letter, index);
}
Console.Write(decimaloutput );
}
static int hextodec(char letter, int index){
string hexcode="0123456789ABCDEF";
int hexval=hexcode.IndexOf(letter);
for(int i=0;i<index;i++){
hexval*=16;
}
return hexval;
}
}
}
3. By ๏ผก๏ผฃ๏ผฅ
Made by ๏ผก๏ผฃ๏ผฅ. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Hexadecimals to Decimals convertor.
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
string hexadecimals = "0123456789ABCDEF";
string input = Console.ReadLine();
long number =0;
int i,pow=0;
for(i=input.Length-1;i>=0;i--){
number+=hexadecimals.IndexOf(input[i])*Convert.ToInt64(Math.Pow(16,pow));
pow++;
}
Console.Write("0x{0} in decimals is {1}.",input,number);
}
}
}
4. By Martin Ferenec
Made by Martin Ferenec. When prompted enter HEX value and the recursive method will convert it to DEC and display it. There is no failsafe in place for characters that do not belong to HEX numeric system so be cautious when entering values otherwise you will get wrong conversion. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MartinFerenec
{
class HEXtoDEC_RecursiveMethod
{
static void Main(string[] args)
{
string HEX1 = Console.ReadLine();
Console.WriteLine(HEXtoDEC(HEX1,HEX1.Length));
}
static int HEXtoDEC(string HEX1, int position) //recursive method
{
int num = 0, pos = position - 1, power = HEX1.Length - (pos + 1);
switch (HEX1[pos])
{
case 'A':
num = 10;
break;
case 'B':
num = 11;
break;
case 'C':
num = 12;
break;
case 'D':
num = 13;
break;
case 'E':
num = 14;
break;
case 'F':
num = 15;
break;
default:
num = HEX1[pos]-48;
break;
}
if (pos == 0) return (num*(int)Math.Pow(16,power));
else return (num * (int)Math.Pow(16, power)) + HEXtoDEC(HEX1, pos);
}
}
}
5. By Rosen Nikolov
Made by Rosen Nikolov. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
public delegate int NumToText(string txt);
class Program
{
static void Main(string[] args)
{
Console.WriteLine(toBase10("C9"));
}
static int toBase10(string base16)
{
var power= 0;
var baseNum=16;
var base10=0;
var base16SpecialSymbols= new Dictionary<char,int>(){{'A',10},{'B',11},
{'C',12},
{'D',14},
{'F',15}};
foreach (var symbol in base16.ToUpper().Reverse())
{
if(!char.IsNumber(symbol))
{
base10+=(int)(base16SpecialSymbols[symbol]*Math.Pow(baseNum,power++));
}else{
base10+=(int)(int.Parse(symbol.ToString())*Math.Pow(baseNum,power++));
}
}
return base10;
}
}
}
6. By Malek Alsset
Made by Malek Alsset. Change 1A at line ‘String hexNum = “1A”;’ to the hexadecimal number you want to convert to decimal. ( Source )
using System;
class MA
{
// Function to convert
// hexadecimal to decimal
static int hexadecimalToDecimal(String hexVal)
{
int len = hexVal.Length;
// Initializing base1 value
// to 1, i.e 16^0
int base1 = 1;
int dec_val = 0;
// Extracting characters as
// digits from last character
for (int i = len - 1; i >= 0; i--)
{
// if character lies in '0'-'9',
// converting it to integral 0-9
// by subtracting 48 from ASCII value
if (hexVal[i] >= '0' &&
hexVal[i] <= '9')
{
dec_val += (hexVal[i] - 48) * base1;
// incrementing base1 by power
base1 = base1 * 16;
}
// if character lies in 'A'-'F' ,
// converting it to integral
// 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i] >= 'A' &&
hexVal[i] <= 'F')
{
dec_val += (hexVal[i] - 55) * base1;
// incrementing base1 by power
base1 = base1 * 16;
}
}
return dec_val;
}
static void Main()
{
String hexNum = "1A";
Console.WriteLine(hexadecimalToDecimal(hexNum));
}
}
7. By MihailNMihaylov
Made by MihailNMihaylov. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace floating_points
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Hexdecimal: ");
string hexValue = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Your number in decimal is: {0} ", Convert.ToInt32(hexValue, 16));
Console.WriteLine();
}
}
}
8. By dievaGit
Made by dievaGit. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_15
{
class Program
{
static void Main(string[] args)
{
//Defining variables
string hexa = "";
char[] array = new char[100];
double sum = 0, x = 1;
//Requesting the number of numbers
Console.Write(" Enter the hexadecimal number: ");
hexa = Console.ReadLine();
array = hexa.ToCharArray();
//converting ti decimal
for(int i = (hexa.Count() - 1); i >= 0; i--)
{
switch(array[i])
{
case 'A':
x = 10;
sum += (Math.Pow(16, i) * x);
break;
case 'B':
x = 11;
sum += (Math.Pow(16, i) * x);
break;
case 'C':
x = 12;
sum += (Math.Pow(16, i) * x);
break;
case 'D':
x = 13;
sum += (Math.Pow(16, i) * x);
break;
case 'E':
x = 14;
sum += (Math.Pow(16, i) * x);
break;
case 'F':
x = 15;
sum += (Math.Pow(16, i) * x);
break;
default:
x = Convert.ToInt32(array[i]);
sum += (Math.Pow(16, i) * x);
break;
}
}
//Showing values
Console.Write(" The hexadecimal number " + hexa + " to decimal is: {0}", sum);
Console.ReadKey();
}
}
}
9. By TobynC
Made by TobynC. ( Source )
using System;
using System.Linq;
using System.Text;
namespace DecimalConverter
{
public class Hexidecimal
{
private readonly string _input;
private string _hexOutput;
public Hexidecimal(string input)
{
_input = input;
_hexOutput = null;
}
public void Convert()
{
var inputArray = _input.ToCharArray();
var sb = new StringBuilder();
foreach (var binary in inputArray.Select(c => new Binary(ConvertValue(c))))
{
binary.ConvertToBinary();
sb.Append(" ");
for (var j = binary.Print().Length; j < 4; j++)
{
sb.Append("0");
}
sb.Append(binary.Print());
}
_hexOutput = sb.ToString();
}
private static long ConvertValue(char c)
{
switch (c)
{
case 'A':
case 'a':
return 10;
case 'B':
case 'b':
return 11;
case 'C':
case 'c':
return 12;
case 'D':
case 'd':
return 13;
case 'E':
case 'e':
return 14;
case 'F':
case 'f':
return 15;
default:
try
{
return long.Parse(c.ToString());
}
catch (Exception e)
{
Console.WriteLine(e);
return -1;
}
}
}
public string Print()
{
return _hexOutput;
}
}
}