struct.unpack null-terminated string

Hans Brand Hans.Brand at BrandInnovators.com
Fri Aug 3 09:37:07 EDT 2001


Hi,

Quoted from the "Python Library Reference", "4.3 struct -- Interpret strings
as packed binary data":

"For the "s" format character, the count is interpreted as the size of the
string, not a repeat count like for the other format characters; e.g. '10s'
means a single 10-byte string, while '10c' means 10 characters. For packing,
the string is truncated or padded with null bytes as appropriate to make it
fit. For unpacking, the resulting string always has exactly the specified
number of bytes. As a special case, '0s' means a single, empty string (while
'0c' means 0 characters)."

So, the 'fmt' string defines the length of the containing string, not the
(if any) terminating null.

HTH, Hans

"Xavier Defrang" <xavier at perceval.net> wrote in message
news:mailman.996832925.27921.python-list at python.org...
>
> Hello,
>
> How do I unpack a null-terminated string with the struct module?  I
> couldn't find it in the lib reference... maybe do i have to upgrade my
> glasses?
>
> Thanks in advance,
>
> Xavier Defrang
> Perceval R&D Team
> xavier at perceval.net
>
> -------------------------------------------------
>  Perceval Technologies SA/NV  Tel: +32-2-6409194
>  Rue Tenbosch, 9              Fax: +32-2-6403154
>  B-1000 Brussels         http://www.perceval.net
>  BELGIUM                       info at perceval.net
> -------------------------------------------------
>
>





More information about the Python-list mailing list