pack a three byte int

John Machin sjmachin at lexicon.net
Fri Nov 10 00:44:00 CET 2006


p.lavarre at ieee.org wrote:
> > Not as concisely as a one-byte struct code
>
> Help, what do you mean?

Help, what did you mean by the question?

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

>
> > you presumably... read... the manual ...
>
> Did I reread the wrong parts?  I see I could define a ctypes.Structure
> since 2.5, but that would be neither concise, nor since 2.3.

Looks like you ignored the first word in the sentence ("Not").

>
> > when 24-bit machines become ... popular
>
> Indeed the struct's defined recently, ~1980, were contorted to make
> them easy to say in C, which makes them easy to say in Python, e.g.:
>
> X28Read10 = 0x28
> cdb = struct.pack('>BBIBHB', X28Read10, 0, skip, 0, count, 0)
>
> But when talking the 1960's lingo I find I am actually resorting to
> horrors like:
>
> X12Inquiry = 0x12
> xxs = [0] * 6
> xxs[0] = X12Inquiry
> xxs[4] = allocationLength
> rq = ''.join([chr(xx) for xx in xxs])
>
> Surely this is wrong?  A failure on my part to think in Python?

It looks wrong (and a few other adjectives), irrespective of what
problem it is trying to solve. Looks like little-endian 4-byte integer
followed by 2-byte integer ... what's wrong with struct.pack("<IH",
X12Inquiry, allocationLength) ????

Your original question asked about bigendian 3-byte integers; have you
read the suggested solution that I posted? Does it do what you asked
(one pack call instead of three)????




More information about the Python-list mailing list