Hex to Decimal

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

namespace HexToDec
{
class Program
{
static void Main(string[] args)
{
string hex = Console.ReadLine();

int Total = 0;

//TBD: Need to check for other characters are entered
for (int i = 0; i <= hex.Length - 1; i++)
{
switch (Convert.ToChar(hex[i].ToString().ToUpper()))
{
case 'A':
Total = Total + 10*((Int32)Math.Pow(16,hex.Length-1-i));
break;
case 'B':
Total = Total + 11 * ((Int32)Math.Pow(16, hex.Length - 1 - i));
break;
case 'C':
Total = Total + 12 * ((Int32)Math.Pow(16, hex.Length - 1 - i));
break;
case 'D':
Total = Total + 13 * ((Int32)Math.Pow(16, hex.Length - 1 - i));
break;
case 'E':
Total = Total + 14 * ((Int32)Math.Pow(16, hex.Length - 1 - i));
break;
case 'F':
Total = Total + 15 * ((Int32)Math.Pow(16, hex.Length - 1 - i));
break;
default:
Total = Total + Convert.ToInt32(hex[i].ToString()) * ((Int32)Math.Pow(16, hex.Length - 1 - i));
break;
}
}

Console.WriteLine(Total);
Console.ReadLine();
}
}
}

Comments

Popular posts from this blog

Network Security: LAN manager authentication level

Cisco AnyConnect Secure Mobility Client - VPN service not available. The VPN agent service is not responding