hex string to hex value
fredrik at pythonware.com
Wed Nov 23 08:04:29 CET 2005
mensanator at aol.com wrote:
> Fredrik Lundh's solution works if the hex string starts with "0x"
that's what "interpret [it] as a Python literal" meant.
> (which it will when the string is created with the hex function).
> >>> int(hex(m),0)
> But it won't work without the "0x".
> >>> int('0x1A',0)
> >>> int('0x1A',16)
> >>> int('1A',16)
> >>> int('1A',0)
> Traceback (most recent call last):
> File "<pyshell#18>", line 1, in -toplevel-
> ValueError: invalid literal for int(): 1A
as the error message says, 1A isn't a valid literal.
More information about the Python-list