Can anyone explain to me why
struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2)
gives 4 bytes?
-Steven 5 2709
On Jun 26, 9:00 am, "Steven Clark" <steven.p.cl...@gmail.comwrote:
Can anyone explain to me why
struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2)
gives 4 bytes?
Alignment -- read the manual.
On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sj******@lexicon.netwrote:
On Jun 26, 9:00 am, "Steven Clark" <steven.p.cl...@gmail.comwrote:
>Can anyone explain to me why struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2) gives 4 bytes?
Alignment -- read the manual.
-- http://mail.python.org/mailman/listinfo/python-list
If "the manual" is the help files for the struct module, I've read it
several times over. I understand endianness; I don't understand
alignment. Could anyone give a less cryptic / terse answer?
En Wed, 25 Jun 2008 23:38:54 -0300, Steven Clark
<st************@gmail.comescribi�:
On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sj******@lexicon.net>
wrote:
>On Jun 26, 9:00 am, "Steven Clark" <steven.p.cl...@gmail.comwrote:
>>Can anyone explain to me why struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2) gives 4 bytes?
Alignment -- read the manual.
If "the manual" is the help files for the struct module, I've read it
several times over. I understand endianness; I don't understand
alignment. Could anyone give a less cryptic / terse answer?
A good start might be this Wikipedia article:
<http://en.wikipedia.org/wiki/Data_structure_alignment>
--
Gabriel Genellina
On Jun 26, 12:38*pm, "Steven Clark" <steven.p.cl...@gmail.comwrote:
On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sjmac...@lexicon.netwrote:
On Jun 26, 9:00 am, "Steven Clark" <steven.p.cl...@gmail.comwrote:
Can anyone explain to me why
struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2)
gives 4 bytes?
Alignment -- read the manual.
-- http://mail.python.org/mailman/listinfo/python-list
If "the manual" is the help files for the struct module, I've read it
several times over. I understand endianness; I don't understand
alignment. Could anyone give a less cryptic / terse answer?
google("struct alignment")
For efficiency reasons many CPUs require particular primitive data
types (integers/pointers of various sizes) to be placed in memory at
particular boundaries. For example, shorts ("H" above, usually two bytes
and probably always so in the struct module) are often required to be
on even addresses, and longer objects to be on 4 or 8 byte boundaries.
This allows for much more efficient memory access on many platforms
(of course the rules depend on the platform). Although RAM _appears_ to
the random access to arbitrary bytes, the underlying hardware will often
fetch chunks of bytes in parallel. If a number spanned the boundaries of
such a chunk it would require two fetch cycles instead of one. So
this is avoided for performance reasons.
So, packing "HB" puts a short at offset 0 (even) and then a byte.
Conversely, packing "BH" puts a byte at offset zero but puts the short
at offset 2 (to be even), leaving a gap after the byte to achieve this,
thus the 4 byte size of the result (byte, gap, short).
This layout procedure is called "alignment".
Cheers,
--
Cameron Simpson <cs@zip.com.auDoD#743 http://www.cskk.ezoshosting.com/cs/
Thanks for taking the time to type a detailed, helpful response,
Cameron. Much appreciated! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gordon Scott |
last post by:
Hi All,
I've got a problem I'm seeing when trying to use the struct module to send
data to a different machine.
Actually I'm making a condensed...
|
by: Josiah Carlson |
last post by:
Good day everyone,
I have produced a patch against the latest CVS to add support for two
new formatting characters in the struct module. It is...
|
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of...
|
by: Giovanni Bajo |
last post by:
Hello,
given the ongoing work on struct (which I thought was a dead module), I was
wondering if it would be possible to add an API to register...
|
by: rong.xian |
last post by:
Before I was bitten by the difference below, I think these two ways are
the same.
However, they are not.
Is there any geek who can tell me if this...
|
by: Jansson Christer |
last post by:
Hi all,
I have discovered that in my Python 2.4.1 installation (on Solaris 8),
struct.pack handles things in a way that seems inconsistent to me....
|
by: Jack |
last post by:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean
typedef struct{
int ID;
char name;
}sampleStruct;
...
|
by: Joshua J. Kugler |
last post by:
I'm trying to put some values into a struct. Some of these values are NaN
and Inf due to the nature of the data. As you well may know, struct (and...
|
by: Cameron Simpson |
last post by:
On 25Jun2008 22:38, Steven Clark <steven.p.clark@gmail.comwrote:
| On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sjmachin@lexicon.netwrote:
| On...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |