pack a three byte int

p.lavarre at ieee.org p.lavarre at ieee.org
Thu Nov 9 20:09:04 EST 2006


Speaking as the OP, perhaps I should mention:

> > [-3:] to [1:] is a minor cosmetic improvement

To my eye, that's Not an improvement.

'\x08' '\x01\x23\x45' '\x80' '\0' is the correct pack of (0x08,
0x12345, 0x80, 0) because '\x01\x23\x45' are the significant low three
bytes of a big-endian x12345, thus [-3:].

The [1:] fact that we can keep the 3 significant bytes by tossing
exactly 1 byte away after rounding the bit length of that digital
number up to the nearest power of two which happens to be 4 = 3 + 1 is
merely incidental - not of central significance.




More information about the Python-list mailing list