By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

pack/unpack zero terminated string

P: n/a
Hello,

Thanks for your time.

After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.

By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".

Please, has someone any hint or pointer to another librarian to be
used?

Thanks.

May 2 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
tmp123 a écrit :
Hello,

Thanks for your time.

After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.

By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".

Please, has someone any hint or pointer to another librarian to be
used?
May look at ctypes and its c_char_p type

Documentation says:
http://python.net/crew/theller/ctype...tal-data-types

c_char_p
Represents the C char * datatype, which must be a pointer to a
zero-terminated string. The constructor accepts an integer address, or a
string.

Note: its in standard libraries from Python 2.5.
May 2 '07 #2

P: n/a
tmp123 a écrit :
Hello,

Thanks for your time.

After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.

By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".

Please, has someone any hint or pointer to another librarian to be
used?
May look at xstruct too

http://www.sis.nl/python/xstruct/xstruct.shtml

May 2 '07 #3

P: n/a
On May 3, 12:01 am, Laurent Pointal <laurent.poin...@limsi.frwrote:
tmp123 a écrit :
Hello,
Thanks for your time.
After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.
By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".
Please, has someone any hint or pointer to another librarian to be
used?

May look at xstruct too

http://www.sis.nl/python/xstruct/xstruct.shtml
Hi, Laurent,

It's a reasonable presumption that the OP needs to unpack *variable-
length* zero-terminated strings, otherwise why is he asking? This
would need a new format type e.g. "z".

xstruct doesn't appear to offer variable-length strings, and is frozen
in time (October 1999) -- inspection of the source shows that it is a
copy of Python 1.5.2 structmodule.c with added stuff.

The OP might like to try a bit of DIY in Python, along the following
lines:

C:\junk>type unpackz.py
def getz(strg, start=0):
zpos = strg.index('\0', start)
return strg[start:zpos], zpos + 1

def getB(strg, start=0):
return ord(strg[start]), start + 1

def unpack_BzzB(strg):
pos = 0
r0, pos = getB(strg, pos)
r1, pos = getz(strg, pos)
r2, pos = getz(strg, pos)
r3, pos = getB(strg, pos)
assert pos == len(strg)
return r0, r1, r2, r3

x = chr(42) + 'foo\0' + 'mumble\0' + '\xff'
print unpack_BzzB(x)
print unpack_BzzB('\0' * 4)

C:\junk>unpackz.py
(42, 'foo', 'mumble', 255)
(0, '', '', 0)

HTH,
John

May 2 '07 #4

P: n/a
On May 2, 11:13 pm, John Machin <sjmac...@lexicon.netwrote:
On May 3, 12:01 am, Laurent Pointal <laurent.poin...@limsi.frwrote:


tmp123a écrit :
Hello,
Thanks for your time.
After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.
By example, if the packed data contains: byte + zero terminated string
+ zero terminated string + byte, it seems no possible to unpack it
using "struct".
Please, has someone any hint or pointer to another librarian to be
used?
May look at xstruct too
http://www.sis.nl/python/xstruct/xstruct.shtml

Hi, Laurent,

It's a reasonable presumption that the OP needs to unpack *variable-
length* zero-terminated strings, otherwise why is he asking? This
would need a new format type e.g. "z".

xstruct doesn't appear to offer variable-length strings, and is frozen
in time (October 1999) -- inspection of the source shows that it is a
copy of Python 1.5.2 structmodule.c with added stuff.

The OP might like to try a bit of DIY in Python, along the following
lines:

C:\junk>type unpackz.py
def getz(strg, start=0):
zpos = strg.index('\0', start)
return strg[start:zpos], zpos + 1

def getB(strg, start=0):
return ord(strg[start]), start + 1

def unpack_BzzB(strg):
pos = 0
r0, pos = getB(strg, pos)
r1, pos = getz(strg, pos)
r2, pos = getz(strg, pos)
r3, pos = getB(strg, pos)
assert pos == len(strg)
return r0, r1, r2, r3

x = chr(42) + 'foo\0' + 'mumble\0' + '\xff'
print unpack_BzzB(x)
print unpack_BzzB('\0' * 4)

C:\junk>unpackz.py
(42, 'foo', 'mumble', 255)
(0, '', '', 0)

HTH,
John
Hello John,

Totally true, the solution you propose is the one I'm using now. The
subject was, before to start from scratch, try to reuse something
existing.

Another possibility was to modify the "struct" package with the new
option, but it seems a mixed C-Python implementation, and I do not
like to start having compatibility problems in the C elements.

Kind regards.

May 3 '07 #5

P: n/a
tmp123 <tm****@menta.netwrote:
>
After review the "struct" documentation, it seems there are no option
to pack/unpack zero terminated strings.
Right. Just as there is no way to describe such a thing as a C struct.
You'll have to unpack the fields by hand, which is that case won't be hard.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.