Re: pack a three byte int



p.lavarre@xxxxxxxx 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)????

.



Relevant Pages

  • Re: which feature of python do you like most?
    ... I've never used Perl, but I know other c-like laguages, and I can tell ... you what I like about python: ... - It is concise, ...
    (comp.lang.python)
  • Re: list index()
    ... Python 3. ... obsolete by "in", which is faster and more concise. ... Is there really some reason "key" IN dict can be implemented ...
    (comp.lang.python)
  • Py2exe Question - Deployment Advice
    ... I wrote my first Python script today and was very impressed at how ... concise the code was and development time. ...
    (comp.lang.python)
  • Re: Powers of 5
    ... by successive squaring. ... in Python for the sake of being concise, but now the function takes k ...
    (sci.math)