I am total noob regarding regex
.
My goal is to check whether a string is a valid representation of a hex number.
Currently my implementation (which I find really inefficient) is having a List with all hex digits (0-9, A-F) and checking whether my string contains characters not contained in given List.
I bet this can be easily done using regular expressions but I have no idea how to implement it.
private bool ISValidHEX(string s)
{
List<string> ToCheck = new List<string>();
for (int i = 0; i < 10; i++)
{
ToCheck.Add(i.ToString());
}
ToCheck.Add("A");
ToCheck.Add("B");
ToCheck.Add("C");
ToCheck.Add("D");
ToCheck.Add("E");
ToCheck.Add("F");
for (int i = 0; i < s.Length; i++)
{
if( !ToCheck.Contains(s.Substring(i,1)))
{
return false;
}
}
return true;
}
Our normal counting system only uses the digits 0 through 9. But hexadecimal uses the digits 0 through F. Yes, in hexadecimal, things like A, B, C, D, E, and F are considered numbers, not letters. That means that 200 is a perfectly valid hexadecimal number just as much as 2FA is also a valid hex number.
Return Value from hex()hex() function converts an integer to the corresponding hexadecimal number in string form and returns it. The returned hexadecimal string starts with the prefix 0x indicating it's in hexadecimal form.
The isxdigit() function checks whether a character is a hexadecimal digit character (0-9, a-f, A-F) or not. The function prototype of isxdigit() is: int isxdigit( int arg ); It is defined in the <ctype.
I would have thought that it's quickest to attempt to convert your string to an integral type and deal with any exception. Use code like this:
int num = Int32.Parse(s, System.Globalization.NumberStyles.HexNumber);
The resulting code is possibly easier to follow than a regular expression and is particularly useful if you need the parsed value (else you could use Int32.TryParse
which is adequately documented in other answers).
(One of my favourite quotations is by Jamie Zawinski: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems.")
To simply check
Check if string is valid represantion of HEX number
you may use a method like:
int res = 0;
if(int.TryParse(val,
System.Globalization.NumberStyles.HexNumber,
System.Globalization.CultureInfo.InvariantCulture, out res)) {
//IT'S A VALID HEX
}
Pay attention on System.Globalization.CultureInfo.InvariantCulture
parameter, change it according to your needs.
I recommend to use Int32.TryParse. There is an overload that allow the Hex numbers conversion
int v;
string test = "FF";
if(Int32.TryParse(test, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out v))
Console.WriteLine("Is HEX:" + v.ToString());
This is better than a simple Int32.Parse because, in the case that you have an invalid hex or the conversion overflows the Int32.MaxValue you don't get an exception but you could simply test the boolean return value.
Warning, the string cannot be prefixed with "0x" or "&H"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With