hex string to hex value

Fredrik Lundh 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)
> 66
>
> But it won't work without the "0x".
>
> >>> int('0x1A',0)
> 26
> >>> int('0x1A',16)
> 26
> >>> int('1A',16)
> 26
> >>> int('1A',0)
>
> Traceback (most recent call last):
>   File "<pyshell#18>", line 1, in -toplevel-
>     int('1A',0)
> ValueError: invalid literal for int(): 1A

as the error message says, 1A isn't a valid literal.

</F>






More information about the Python-list mailing list