pack/unpack zero terminated string

T

tmp123

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.
 
L

Laurent Pointal

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/ctypes/reference.html#fundamental-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.
 
L

Laurent Pointal

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
 
J

John Machin

tmp123 a écrit :






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
 
T

tmp123

tmp123a écrit :
May look at xstruct too

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.
 
T

Tim Roberts

tmp123 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top