hex

using System;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string BinaryStr = “1000101001110”;
int Index = 0;
int Decimal = 0;
foreach (char Char in BinaryStr.Reverse())
{
if (Index != 0)
{
Decimal += Index * 2 * Convert.ToInt32(Char.ToString());
Index = Index * 2;
}
else
{
Decimal += Convert.ToInt32(Char.ToString());
Index++;
}
}
Console.WriteLine(“hexadecimal no for : ” + BinaryStr + ” => ” + Decimal.ToString());
string Hexa1 = toHex(Convert.ToInt64(Decimal));
Console.WriteLine(“hexadecimal no for : ” + BinaryStr + ” => ” + Hexa1 );
}
public static string toHex(Int64 d)
{
var r = d % 16;
string result;
if (d – r == 0)
result = toChar(Convert.ToInt32(r));
else
result = toHex((d – r) / 16) + toChar(Convert.ToInt32(r));
return result;
}
public static string toChar(int n)
{
const string alpha = “0123456789ABCDEF”;
return alpha.Substring(n, 1);
}
}