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 +=
Sorry, that should have been:
cdb += struct.pack(L, skip)[1:]
Dave
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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
[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
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)
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
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
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
[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
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:
[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
[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
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
(...) 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
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
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 +=
[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
19 matches
Mail list logo