C Nielsen C Nielsen - 1 month ago 21
Python Question

Python Trailing L Problem

I'm using Python to script some operations on specific locations in memory (32 bit addresses) in an embedded system.

When I'm converting these addresses to and from strings, integers and hex values a trailing L seems to appear. This can be a real pain, for example the following seemingly harmless code won't work:

int(hex(4220963601))


Or this:

int('0xfb96cb11L',16)


Does anyone know how to avoid this?

So far I've come up with this method to strip the trailing L off of a string, but it doesn't seem very elegant:

if longNum[-1] == "L":
longNum = longNum[:-1]

Answer

If you do the conversion to hex using

 "%x" % 4220963601

there will be neither the 0x nor the trailing L.