Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Trailing L Problem

Tags:

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] 
like image 626
C Nielsen Avatar asked May 06 '11 21:05

C Nielsen


People also ask

What does L stand for in Python?

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.

How to take long input in Python?

Type int(x) to convert x to a plain integer. Type long(x) to convert x to a long integer.

How big is a long in Python?

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.

How big is Python integer?

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.


2 Answers

If you do the conversion to hex using

 "%x" % 4220963601 

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

like image 165
Sven Marnach Avatar answered Oct 20 '22 00:10

Sven Marnach


Calling str() on those values should omit the trailing 'L'.

like image 40
yan Avatar answered Oct 19 '22 23:10

yan