Problem with unpack hex to decimal

Artie Gold artiegold at austin.rr.com
Sun Apr 17 23:05:29 EDT 2005


Jonathan Brady wrote:
> <serpent17 at gmail.com> wrote in message 
> news:1113763068.002612.240940 at l41g2000cwc.googlegroups.com...
> 
>>Hello,
>>
>>I was looking at this:
>>http://docs.python.org/lib/module-struct.html
>>and tried the following
>>
>>
>>>>>import struct
>>>>>struct.calcsize('h')
>>
>>2
>>
>>>>>struct.calcsize('b')
>>
>>1
>>
>>>>>struct.calcsize('bh')
>>
>>4
>>
>>I would have expected
>>
>>
>>>>>struct.calcsize('bh')
>>
>>3
>>
>>what am I missing ?
> 
> 
> Not sure, however I also find the following confusing:
> 
>>>>struct.calcsize('hb')
> 
> 3
> 
>>>>struct.calcsize('hb') == struct.calcsize('bh')
> 
> False
> 
> I could understand aligning to multiples of 4, but why is 'hb' different 
> from 'bh'? 
> 
> 
Evidently, shorts need to be aligned at an even address on your 
platform. Consider the following layout, where `b' represents the signed 
char, `h' represents the bytes occupied by the short and `X' represents 
unused bytes (due to alignment.

'bh', a signed char followed by a short would look like:

bXhh -- or four bytes, but 'hb', a short followed by a signed char would be:

hhb (as `char' and its siblings have no alignment requirements)

HTH,
--ag

-- 
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays



More information about the Python-list mailing list