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]
Python supports arbitrary precision integers, meaning you're able to represent larger numbers than a normal 32 or 64 bit integer type. The L tells you when a literal is of this type and not a regular integer. Note, that L only shows up in the interpreter output, it's just signifying the type.
Type int(x) to convert x to a plain integer. Type long(x) to convert x to a long integer.
Python currently distinguishes between two kinds of integers (ints): regular or short ints, limited by the size of a C long (typically 32 or 64 bits), and long ints, which are limited only by available memory.
x - the int type is now of unlimited length by default. You don't have to specify what type of variable you want; Python does that automatically. Int: The basic integer type in python, equivalent to the hardware 'c long' for the platform you are using in Python 2. x, unlimited in length in Python 3.
If you do the conversion to hex using
"%x" % 4220963601
there will be neither the 0x
nor the trailing L
.
Calling str()
on those values should omit the trailing 'L'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With