Re: pack a three byte int

2006-11-09 Thread Gabriel Genellina
At Friday 10/11/2006 00:08, [EMAIL PROTECTED] wrote: > > >>> import binascii > > >>> cdb = binascii.unhexlify('%02X%06X%02X%02X' % (0x08, 0x12345, 0x80, 0)) > > >>> binascii.hexlify(cdb) > >'080123458000' > > The only problem I can see is that this code is endianness-dependent; > the suggested

Re: pack a three byte int

2006-11-09 Thread p . lavarre
> > ... 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 can see is that this code is endianness-dependent; > the suggested versions using

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 pro

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 significan

Re: pack a three byte int

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

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: > > "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 does

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 x1234

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

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

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

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'

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 t

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 impressio

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

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 wh