pack a three byte int

p.lavarre at ieee.org p.lavarre at ieee.org
Thu Nov 9 20:03:00 EST 2006


> > when talking the 1960's lingo
> > ...
> > X12Inquiry = 0x12
> > xxs = [0] * 6
> > xxs[0] = X12Inquiry
> > xxs[4] = allocationLength
> > rq = ''.join([chr(xx) for xx in xxs])
>
> It looks wrong (and a few other adjectives),

Ah, we agree, thank you for saying.

> Looks like little-endian 4-byte integer
> followed by 2-byte integer ... what's wrong with struct.pack("<IH",
> X12Inquiry, allocationLength) ????

Pack '<IH' doesn't match how the code that I'm refactoring thinks about
these things.

The people who wrote this stuff forty years ago were thinking of bit
fields - here bit lengths of 8 then 3 then 21 then 8 then 8 bits -
cheating only when the bit boundaries happened to hit byte boundaries.

Yes, as you describe in this example, I could cheat when the boundaries
happen to hit H or I boundaries as well, but then I'm still left coping
with the cases where the fields split on byte boundaries that are not H
or I boundaries, such as the example:

> > skip = 0x123456; count = 0x80
> > hi, lo = divmod(skip, 0x10000)
>
> Does it do what you asked (one pack call instead of three)????

One pack call, not three, yes.

Shatters the 3 byte int into 1 and 2 bytes by divmod of (0xFFFF + 1),
yes.

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