Converting a hex string to a number

Alex Martelli aleax at aleax.it
Wed Jul 10 03:45:16 EDT 2002


Matt Gerrans wrote:

>> >> eval!
>> >
>> >Overkill. Unsafe. Ugly. Lame.
>> >
>>
>> What do you mean by unsafe?
> 
> Also, in addition to the criticism of the suggested method (eval), you
> could
> post your simpler, safe and able-bodied alternative.   Perhaps it was:
> 
> try:
>    n = eval(string)
> except:
>    n = 0

The one obvious way to perform the task asked for in the subject.
and clarified by the original poster, has been posted since MANY posts ago
in this thread, and it's n = str(thestring, 0) [you can also use
16 as the second argument if you're sure about the 'hex' part -- 0
looks for a leading 0x as indicator of hex-ness].


Alex



More information about the Python-list mailing list