hex check

Raymond Hettinger vze4rx4y at verizon.net
Wed Jul 16 05:02:52 EDT 2003


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

Try converting it.
If the conversion fails, then the format was bad:

>>> int('0x2F', 16)
47
>>> int('0x2G', 16)

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in -toplevel-
    int('0x2G', 16)
ValueError: invalid literal for int(): 0x2G



Raymond Hettinger






More information about the Python-list mailing list