473,799 Members | 2,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7368
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(str g):
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...@lexic on.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(str g):
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.n etwrote:
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2321
by: Johannes | last post by:
Hi, I tried to pack eight integer values and one string into one binary string, which I would like to store in a mysql db. I encountered two problems doing this: 1) $this->packed = pack('N8A*', $this->value1, $this->value2, $this->value3, $this->value4, $this->value5, $this->value6, $this->value7, $this->value8, $this->stringvalue);
2
3696
by: Sergey Dorofeev | last post by:
I can use string.unpack if string in struct uses fixed amount of bytes. But is there some extension to struct modue, which allows to unpack zero-terminated string, size of which is unknown? E.g. such struct: long, long, some bytes (string), zero, short, short, short.
4
15610
by: Stacy Mader | last post by:
Greetings all, I have a VMS binary file with weather data. The record on each line is 1xint(8) 12xint(2) Using the perl unpack function, I can decode the binary file like this: <snip>
18
7586
by: p.lavarre | last post by:
Can Python not express the idea of a three-byte int? For instance, in the working example below, can we somehow collapse the three calls of struct.pack into one? 08 12 34 56 80 00 I ask because I'm trying to refactor working code that is concise: 08 12 34 56 80 00
0
1389
by: Steve | last post by:
Are there any gurus of the pack/unpack world out there that can help me with this one? I have an encrypted string which I'm reading from a zipped file. It was written using pack ( "la" . strlen (encrypted), strlen (encrypted), strlen (encrypted)) most of the time, it decrypts correctly when I unpack as...
5
1744
by: andmarti | last post by:
If I build a strict with: import struct print struck.pack ('i', 1) it returns a '\n'. What's wrong with it??? :( --
0
1431
by: kardon33 | last post by:
Ok what i am trying to do with my script i send a poll and receive a reply and read that reply. I have the Poll work properly but what i need help with is trying to convert the reply from ascii to regular text. My reply consist of 30 different variables and i need them to be separated. my code so far is. #!/usr/bin/perl print "Content-type:text/html\n\n"; use IO::Socket; #Poll
1
3159
by: joe shoemaker | last post by:
If you have the following: data = unpack('>L', sock.recv(4)) Does this line of code means that incoming data is big endian and unpack it to endianess of local machine? If local machine is little endian, then big endian is automatically converted to little endian format? thank you.
17
2320
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='".$file_name."&order_by=carnumber'>Carnumber</a></th> <th><a href='".$file_name."&order_by=location'>Location</a></th> <th><a href='".$file_name."&order_by=sighting_date_asc'>Sighting Date</a></th> <th><a href='".$file_name."&order_by=classification'>Code</a></th>...
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.