using System.Globalization; bool valid = int.TryParse(inputString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result);
works for hexadecimal numbers without a prefix. If you do not know what type of number to expect, you can use
bool isHex = inputString.Length > 2 && inputString.Substring(0, 2).ToLowerInvariant() == "0x" && int.TryParse(inputString.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result);
check and parse the string at the same time. For binary, I use
Regex.IsMatch(inputString, "^[01]+$");
You should use inputString = inputString.Trim() to make the application more tolerant of "non-standard input."
source share