hex check

Fredrik Lundh fredrik at pythonware.com
Wed Jul 16 05:00:44 EDT 2003


Ruslan Spivak wrote:

> Does anybody have a hint how to check if input hex number
> is in correct hex format?

not unless you define what a "correct hex format" is, of
course.  but one of these might work:

    import re
    if not re.match("[0-9a-fA-F]+$", number):
        print "oops"

    import re
    if not re.match("0x[0-9a-fA-F]+$", number):
        print "oops"

    try:
        int(number, 16)
    except ValueError:
        print "oops"

(etc)

</F>








More information about the Python-list mailing list