Converting a hex string to a number

Yigal Duppen yduppen at xs4all.nl
Tue Jul 9 08:30:21 EDT 2002


>  I am trying to convert a hex string (for example: "0x12345678") to a
> number (0x12345678 or just 12345678).

You can use int(), with 16 as the value for the second argument (radix) 
>>> int("0x1A2B", 16)
6699

Of course, if the number is very large, this won't work. In this case, 
use long

>>> int("0x123456789ABCDEF", 16)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: int() literal too large: 0x123456789ABCDEF
>>> long("0x123456789ABCDEF", 16)
81985529216486895L

YDD
-- 
.sigmentation Fault



More information about the Python-list mailing list