ishexdigit()
Ben Finney
bignose-hates-spam at and-benfinney-does-too.id.au
Sun Dec 28 23:05:41 EST 2003
On Sun, 28 Dec 2003 18:15:49 -0800, engsolnom at ipns.com wrote:
> Also, if I have a string 4 chars long, representing two bytes of hex,
> how can I *validate* the string as a *legal* hex string?
>
> isdigit works until string = '001A', for example
> isalnum works, but then allows 'foob'
>
> Is there a 'ishexdigit'? I could parse the string, but that seems
> "un-Pythonish"
More Pythonish is to use string.hexdigits and map() to write your own:
>>> import string
>>> def ishexdigit( char ):
... result = ( char in string.hexdigits )
... return result
...
>>> def ishexstring( str ):
... resultmap = map( ishexdigit, str )
... result = ( False not in resultmap )
... return result
...
>>> ishexstring( '010101' )
True
>>> ishexstring( 'deadb00f' )
True
>>> ishexstring( 'foob' )
False
--
\ "For mad scientists who keep brains in jars, here's a tip: why |
`\ not add a slice of lemon to each jar, for freshness?" -- Jack |
_o__) Handey |
Ben Finney <http://bignose.squidly.org/>
More information about the Python-list
mailing list