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 2797
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 file that gets transferred to and read on a
BREW...
|
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 currently an RFE,
which I include a link to at the...
|
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
concept" program to test serializing a structure...
|
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 custom parsing
codes for struct. Whenever I use it...
|
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 is a bug?
(some weird '\x00\x00' was inserted...
|
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.
I haven't found any comprehensible...
|
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;
int main(){
|
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
other things) in Python <= 2.4 doesn't support...
|
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 Jun 26, 9:00 am, "Steven Clark"...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |