hex check
Troels Therkelsen
t_therkelsen at hotmail.com
Wed Jul 16 05:09:26 EDT 2003
In article <mailman.1058340712.8247.python-list at python.org>, Ruslan Spivak wrote:
> Hello.
>
> Does anybody have a hint how to check if input hex number is in correct
> hex format?
>
> Thanks in advance.
You could try the int() built-in function:
Python 2.3b2 (#1, Jun 30 2003, 13:04:39)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int("f3", 16)
243
>>> int("0xf3", 16)
243
>>> int("f3g", 16)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): f3g
>>>
You'd have to wrap it in a construct like this, for example:
inputhex = "f3"
try:
i = int(inputhex, 16)
except ValueErr:
print "Bad hex input: %s" % inputhex
Hope this hint helps,
Troels Therkelsen.
More information about the Python-list
mailing list