pack a three byte int

p.lavarre at p.lavarre at
Fri Nov 10 01:58:20 CET 2006

> "struct" == "Python struct module"
> Struct module has (concise) codes B, H, I, Q for unsigned integers of
> lengths 1, 2, 4, 8, but does *not* have a code for 3-byte integers.

I thought that's what the manual meant, but I was unsure, thank you.

> > > 1. Not as concisely as a one-byte struct code
> Looks like you ignored the first word in the sentence ("Not").

I agree I have no confident idea of what your English meant.

I guess you're hinting at the solution you think I should find obvious,
without volunteering what that is.

Yes?  If so, then:

I guess for you "a one-byte struct code" is a 'B' provided as a "format
character" of the fmt parameter of the struct.pack function.

Yes?  if so, then:

You recommend shattering the three byte int:

skip = 0x012345 ; count = 0x80
struct.pack('>6B', 0x08, skip >> 0x10, skip >> 8, skip, count, 0)

Except you know that chokes over:

DeprecationWarning: 'B' format requires 0 <= number <= 255

So actually you recommend:

def lossypack(fmt, *args):
        return struct.pack(fmt, *[(arg & 0xFF) for arg in args])
skip = 0x012345 ; count = 0x80
lossypack('>6B', 0x08, skip >> 0x10, skip >> 8, skip, count, 0)


> > I guess you're asking me ...
> > to show more plainly that indeed I am trying
> > to make sense of every word of every answer

Am I helping?

More information about the Python-list mailing list