How to convert a "long in a string" to a "long"?

Carsten Haese carsten at uniqsys.com
Fri Nov 18 17:10:06 CET 2005


On Fri, 2005-11-18 at 11:04, ondekoza at gmail.com wrote:
> Hello,
> 
> I need to convert the string "FFFFFFFF" to a long. To convert this
> string I tried the following:
> >>> 0xffffffff
> -1
> >>> 0xffffffffL
> 4294967295L
> 
> OK, this is what I want, so I tried
> 
> s = long("0xffffffffL")
> ValueError: invalid literal for long(): 0xffffffffL
> 
> s = long("0xffffffff")
> ValueError: invalid literal for long(): 0xffffffffL
> 
> What can I do? 
> 
> Thank you in advance.
> Stefan

Leave out the "0x" prefix and tell long() that you're using base 16:

>>> long("ffffffff", 16)
4294967295L

HTH,

Carsten.




More information about the Python-list mailing list