Printing a long

Robert Cragie rcc at nospamthanks_jennic.com
Wed Apr 12 06:23:25 EDT 2000


I can't seem to print a long. The following shows the inconsistencies:

print '%08x' % 0xffffffff
ffffffff
>>> print '%08x' % 0xffffffffL
Traceback (innermost last):
  File "<pyshell#1>", line 1, in ?
    print '%08x' % 0xffffffffL
OverflowError: long int too long to convert
>>> import string
>>> print '%08x' % string.atoi(hex(0xffffffffL)[:-1], 0)
ffffffff
>>> print '%08x' % string.atoi(hex(0xffffffffL)[2:-1], 16)
Traceback (innermost last):
  File "<pyshell#4>", line 1, in ?
    print '%08x' % string.atoi(hex(0xffffffffL)[2:-1], 16)
ValueError: atoi() literal too large: FFFFFFFF
>>>

I realise 0xffffffff is a (signed) int (i.e. -1), whereas 0xffffffffL is a
(signed) long int (i.e. +4294967295)

The problem originally arose from using struct.pack() and struct.unpack()
with 'unsigned' parameters - these are represented by Python longs, but C is
only really concerned about the number of bits, so I have used the signed
representation (which then uses Python ints); this solves my original
problem.

However, I'd be interested to see comments about these apparent
inconsistencies.

Robert Cragie





More information about the Python-list mailing list