[issue7273] struct.pack adding extra '\x00' character in very specific case

Mark Dickinson report at bugs.python.org
Fri Nov 6 17:25:44 CET 2009


Mark Dickinson <dickinsm at gmail.com> added the comment:

I don't think this is a bug:  struct.pack deliberately adds padding bytes 
so that the byte sequence matches the way that a corresponding C struct 
would be stored in memory, on that platform.  This is described 
(admittedly rather briefly) in the documentation at:

http://docs.python.org/library/struct.html

"""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)."""

Depending on your application, you may want to use standard size and 
alignment instead, e.g., with:

struct.pack("<17scBH", ...)

----------
assignee:  -> mark.dickinson
nosy: +mark.dickinson
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7273>
_______________________________________


More information about the Python-bugs-list mailing list