// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; class MainClass { private static string DIGITS = "0123456789ABCDEF"; private static int FromAny(string s, int radix) { int res = 0; char[] sa = s.ToCharArray(); for (int i = 0; i < s.Length; i++) { res = res * radix + DIGITS.IndexOf(sa[i]); } return res; } private static string ToAny(int i, int radix) { string res = ""; int tmp = i; while (tmp > 0) { res = DIGITS.ToCharArray()[tmp % radix] + res; tmp = tmp / radix; } return res; } public static int FromDec(string s) { return FromAny(s, 10); } public static string ToDec(int i) { return ToAny(i, 10); } public static int FromHex(string s) { return FromAny(s, 16); } public static string ToHex(int i) { return ToAny(i, 16); } public static int FromBin(string s) { return FromAny(s, 2); } public static string ToBin(int i) { return ToAny(i, 2); } public static void Main(string[] args) { Console.WriteLine(ToBin(FromHex("FF"))); Console.WriteLine(ToBin(FromDec("11"))); } }