pack a three byte int

p.lavarre at ieee.org p.lavarre at ieee.org
Wed Nov 8 18:10:01 EST 2006


Can Python not express the idea of a three-byte int?

For instance, in the working example below, can we somehow collapse the
three calls of struct.pack into one?

>>> import struct
>>>
>>> skip = 0x123456 ; count = 0x80
>>>
>>> cdb = ''
>>> cdb += struct.pack('>B', 0x08)
>>> cdb += struct.pack('>I', skip)[-3:]
>>> cdb += struct.pack('>BB', count, 0)
>>>
>>> print ' '.join(['%02X' % ord(xx) for xx in cdb])
08 12 34 56 80 00
>>>

I ask because I'm trying to refactor working code that is concise:

>>> cdb0 = '\x08' '\x12\x34\x56' '\x80' '\0'
>>> print ' '.join(['%02X' % ord(xx) for xx in cdb0])
08 12 34 56 80 00
>>>

Thanks in advance, Pat LaVarre




More information about the Python-list mailing list