Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is valid representation of hex number

Tags:

c#

regex

hex

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;
}
like image 378
apomene Avatar asked Sep 19 '14 12:09

apomene


People also ask

Is a valid hex number?

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.

How do you check hex in python?

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.

How do you check if a number is hexadecimal or not?

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.


3 Answers

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.")

like image 54
Bathsheba Avatar answered Nov 06 '22 04:11

Bathsheba


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.

like image 41
Tigran Avatar answered Nov 06 '22 06:11

Tigran


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"

like image 29
Steve Avatar answered Nov 06 '22 04:11

Steve