question on struct.calcsize

John Purser NO_SPAM_jmpurser2 at attbi.com
Thu Mar 28 10:27:16 EST 2002


"Marcus Stojek" <stojek at part-gmbh.de> wrote in message
news:3ca33d3f.26101031 at news.easynews.net...
> Hi,
> could anyone explain the following, please.
> (Win NT, Python 2.1.1)
>
> >>> from struct import *
> >>> calcsize("i")
> 4
> >>> calcsize("s")
> 1
> >>> calcsize("si")
> 8
>
> Thanks,
> Marcus

I got stuck here and this list helped me out.  Here's what I do now.  At the
front of my format string I put a '>' to force big endian format.  From the
struct module documentation:

"""By default, C numbers are represented in the machine's native format and
byte order, and properly aligned by skipping pad bytes if necessary
(according to the rules used by the C compiler).

Alternatively, the first character of the format string can be used to
indicate the byte order, size and alignment of the packed data, according to
the following table:

Character  Byte order  Size and alignment
@      native native
=      native standard
<      little-endian standard
>      big-endian standard
!      network (= big-endian) standard """

One other gotcha was in not opening my binary source file with a 'rb'.  When
I used a simple 'r' a control character in the incoming string stopped the
read 3 characters short of a record.  It took me quite a while with a bit
editor to figure out what I was running into.

Good luck.

John Purser






More information about the Python-list mailing list