Re: pack a three byte int

2006-11-09 Thread Dave Opstad
In article [EMAIL PROTECTED], [EMAIL PROTECTED] wrote: Can Python not express the idea of a three-byte int? For instance, in the working example below, can we somehow collapse the three calls of struct.pack into one? import struct skip = 0x123456 ; count = 0x80 cdb = '' cdb +=

Re: pack a three byte int

2006-11-09 Thread Dave Opstad
Sorry, that should have been: cdb += struct.pack(L, skip)[1:] Dave -- http://mail.python.org/mailman/listinfo/python-list

Re: pack a three byte int

2006-11-09 Thread John Machin
Dave Opstad wrote: Sorry, that should have been: cdb += struct.pack(L, skip)[1:] L and I produce exactly the same 4-byte result. The change from [-3:] to [1:] is a minor cosmetic improvement, but obscures the underlying ... a bit like putting mascara on a pig. I got the impression that the

Re: pack a three byte int

2006-11-09 Thread p . lavarre
Not as concisely as a one-byte struct code Help, what do you mean? 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. when 24-bit machines become ... popular Indeed the

Re: pack a three byte int

2006-11-09 Thread p . lavarre
cdb0 = '\x08' '\x01\x23\x45' '\x80' '\0' cdb = '' cdb += struct.pack('B', 0x08) cdb += struct.pack('I', skip)[-3:] cdb += struct.pack('BB', count, 0) The change from [-3:] to [1:] is a minor cosmetic improvement, Ouch, [1:] works while sizeof I is 4, yes, but that's not what I

Re: pack a three byte int

2006-11-09 Thread John Machin
[EMAIL PROTECTED] 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

Re: pack a three byte int

2006-11-09 Thread p . lavarre
Help, what did you mean by the question? How does Python express the idea: i) Produce the six bytes '\x08' '\x01\x23\x45' '\x80' '\0' at run-time when given the tuple (0x08, 0x12345, 0x80, 0). ii) Produce the six bytes '\x12' '\0\0\0' '\x24' '\0' when given the tuple (0x12, 0, 0x24, 0). iii)

Re: pack a three byte int

2006-11-09 Thread p . lavarre
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

Re: pack a three byte int

2006-11-09 Thread p . lavarre
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

Re: pack a three byte int

2006-11-09 Thread p . lavarre
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

Re: pack a three byte int

2006-11-09 Thread John Machin
[EMAIL PROTECTED] wrote: 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. If it doesn't have a

Re: pack a three byte int

2006-11-09 Thread p . lavarre
Perhaps Python can't concisely say three-byte int ... But Python can say six-nybble hex: import binascii cdb = binascii.unhexlify('%02X%06X%02X%02X' % (0x08, 0x12345, 0x80, 0)) binascii.hexlify(cdb) '080123458000' Thanks again for patiently helping me find this. A shortcut is:

Re: pack a three byte int

2006-11-09 Thread John Machin
[EMAIL PROTECTED] wrote: 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

Re: pack a three byte int

2006-11-09 Thread John Machin
[EMAIL PROTECTED] wrote: 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

Re: pack a three byte int

2006-11-09 Thread Gabriel Genellina
At Thursday 9/11/2006 22:24, [EMAIL PROTECTED] wrote: Perhaps Python can't concisely say three-byte int ... But Python can say six-nybble hex: import binascii cdb = binascii.unhexlify('%02X%06X%02X%02X' % (0x08, 0x12345, 0x80, 0)) binascii.hexlify(cdb) '080123458000' The only problem I

Re: pack a three byte int

2006-11-09 Thread p . lavarre
(...) not. But this may not be of concern to you. Thanks for cautioning us. I suspect we agree: i) pack('...') can't say three byte int. ii) binascii.hexlify evals bytes in the order printed. iii) %X prints the bytes of an int in big-endian order. iv) struct.unpack '' of struct.pack '' flips the bytes of an int v

Re: pack a three byte int

2006-11-09 Thread Gabriel Genellina
pack(...) not. But this may not be of concern to you. Thanks for cautioning us. I suspect we agree: i) pack('...') can't say three byte int. ii) binascii.hexlify evals bytes in the order printed. iii) %X prints the bytes of an int in big-endian order. iv) struct.unpack '' of struct.pack '' flips

pack a three byte int

2006-11-08 Thread p . lavarre
Can Python not express the idea of a three-byte int? For instance, in the working example below, can we somehow collapse the three calls of struct.pack into one? import struct skip = 0x123456 ; count = 0x80 cdb = '' cdb += struct.pack('B', 0x08) cdb += struct.pack('I', skip)[-3:] cdb +=

Re: pack a three byte int

2006-11-08 Thread John Machin
[EMAIL PROTECTED] wrote: Can Python not express the idea of a three-byte int? It is a bit hard to determine what that (rhetorical?) question means. Possible answers: 1. Not as concisely as a one-byte struct code -- as you presumably have already determined by reading the manual ... 2. No, but