hex check

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Jul 16 05:04:18 EDT 2003


Ruslan Spivak <alienoid at is.lg.ua> wrote in 
news:mailman.1058340712.8247.python-list at python.org:

> Does anybody have a hint how to check if input hex number is in correct 
> hex format?
> 
Use 'int' to convert it to a number, catch the exception if it isn't a 
valid format:

>>> int('aa',16)
170
>>> int('0xaa',16)
170
>>> int('ag',16)
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in ?
    int('ag',16)
ValueError: invalid literal for int(): ag
>>> 


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list