csharp Code:
public static class Extensions
{
public static bool TryBinaryParse(this string hexInput, ref string result)
{
try
{
if (!System.Text.RegularExpressions.Regex.Match(hexInput,
"^[A-Fa-f0-9]+$").Success)
{
return false;
}
hexInput = hexInput.ToUpper();
Dictionary<char, string> lookup = new Dictionary<char, string>(16);
{
lookup.Add('0', "0000");
lookup.Add('1', "0001");
lookup.Add('2', "0010");
lookup.Add('3', "0011");
lookup.Add('4', "0100");
lookup.Add('5', "0101");
lookup.Add('6', "0110");
lookup.Add('7', "0111");
lookup.Add('8', "1000");
lookup.Add('9', "1001");
lookup.Add('A', "1010");
lookup.Add('B', "1011");
lookup.Add('C', "1100");
lookup.Add('D', "1101");
lookup.Add('E', "1110");
lookup.Add('F', "1111");
}
result = string.Join(string.Empty,
hexInput.ToCharArray().Select((c) => lookup[c]).ToArray());
return true;
}
catch
{ return false; }
}
}