NEWBIE: What's the instance name?

David M. Wilson dw-google.com at botanicus.net
Mon Dec 29 06:46:36 CET 2003


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"


Have you considered using the int() builtin? Here is an example:

    def is_hex(n):
        '''
        Return truth if <n> can be converted as hexadecimal
        from a string.
        '''

        try:
            int(n, 16)
            return True

        except ValueError:
            return False




More information about the Python-list mailing list