473,895 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct.pack behavior

Can anyone explain to me why
struct.pack('HB ',1,2) gives 3 bytes, whereas struct.pack('BH ',1,2)
gives 4 bytes?

-Steven
Jun 27 '08 #1
5 2821
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.
Jun 27 '08 #2
On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sj******@lexic on.netwrote:
On Jun 26, 9:00 am, "Steven Clark" <steven.p.cl... @gmail.comwrote :
>Can anyone explain to me why
struct.pack('H B',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?
Jun 27 '08 #3
En Wed, 25 Jun 2008 23:38:54 -0300, Steven Clark
<st************ @gmail.comescri bi�:
On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sj******@lexic on.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.or g/wiki/Data_structure_ alignment>

--
Gabriel Genellina

Jun 27 '08 #4
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...@lexic on.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")
Jun 27 '08 #5
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.auD oD#743
http://www.cskk.ezoshosting.com/cs/

Thanks for taking the time to type a detailed, helpful response,
Cameron. Much appreciated!
Jun 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3656
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 enabled cell-phone, essentially I'm trying to format content as if it were being streamed over a socket. So I am trying to write a string by first sending a two-byte length of the
0
2306
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 end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It includes support to pack and unpack the c types: byte, char, short, long, long long, char, *, and...
3
2123
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 containing pointers into a "flattened" bit stream. Here is my code (it dosen't work - compiles fine, pack appears to work, but unpack retrieves jibberish and causes program to crash).
10
2287
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 for non-trivial tasks, I always happen to write small wrapper functions to adjust the values returned by struct. An example API would be the following: ============================================
2
1702
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 between '0123456789abcd' and 6 ) '\x01\x00\x00\x00%\x910\x01\x0e\x00\x00\x000123456789abcd\x00\x00\x06\x00\x00\x00' '\x01\x00\x00\x00%\x910\x01\x0e\x00\x00\x000123456789abcd\x06\x00\x00\x00'
2
10850
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 documentation over known issues with Python 2.4.1 so I try this... Here's the thing:
6
26739
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(){
4
2469
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 inf and nan float values. You get the dreaded "SystemError: frexp() result out of range" error. Before I go and write my own little wrapper, has anyone out there written an "extended" struct that supports the inf and nan values? I know this is...
0
985
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" <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
0
9990
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11249
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10847
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10473
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9651
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7179
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5865
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4693
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.