开发者

Python Trailing L Problem

开发者 https://www.devze.com 2023-03-03 05:33 出处:网络
I\'m using Python to script some operations on specific locations in memory (32 bit addresses) in an embedded system.

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 开发者_C百科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]


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'.


Consider using rstrip. For example:

result.rstrip("L")


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


this could help somebody:

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

精彩评论

暂无评论...
验证码 换一张
取 消