C Nielsen
C Nielsen

Reputation: 562

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]

Upvotes: 25

Views: 33435

Answers (5)

Conor Livingston
Conor Livingston

Reputation: 955

Consider using rstrip. For example:

result.rstrip("L")

Upvotes: 3

docpkd
docpkd

Reputation: 551

This is what I did: int(variable_which_is_printing_as_123L) and it worked for me. This will work for normal integers.

Upvotes: 0

tedy58-user1045284
tedy58-user1045284

Reputation: 19

this could help somebody:

>>>n=0xaefa5ba7b32881bf7d18d18e17d3961097548d7cL
>>>print "n=","%0s"%format(n,'x').upper()
n= AEFA5BA7B32881BF7D18D18E17D3961097548D7C

Upvotes: -1

Sven Marnach
Sven Marnach

Reputation: 601539

If you do the conversion to hex using

 "%x" % 4220963601

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

Upvotes: 24

yan
yan

Reputation: 20982

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

Upvotes: 13

Related Questions