469,338 Members | 8,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

pack/unpack zero terminated string

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
5 6225
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
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
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
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
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.

Similar topics

2 posts views Thread by Sergey Dorofeev | last post: by
4 posts views Thread by Stacy Mader | last post: by
18 posts views Thread by p.lavarre | last post: by
reply views Thread by Steve | last post: by
5 posts views Thread by andmarti | last post: by
reply views Thread by kardon33 | last post: by
1 post views Thread by joe shoemaker | last post: by
17 posts views Thread by JRough | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.