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. 2 3666
"Sergey Dorofeev" <se****@fidoman.ru> wrote in message
news:d9***********@gavrilo.mtu.ru... I can use string.unpack if string in struct uses fixed amount of bytes.
I presume you mean struct.unpack(format, string). The string len must be
known when you call, but need not be fixed across multiple calls with
different strings.
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.
Size is easy to determine. Given the above and string s (untested code):
prelen = struct.calcsize('2l')
strlen = s.find('\0', prelen) - prelen
format = '2l %ds h c 3h' % strlen # c swallows null byte
Note that C structs can have only one variable-sized field and only at the
end. With that restriction, one could slice and unpack the fixed stuff and
then directly slice out the end string. (Again, untested)
format = 2l 3h' # for instance
prelen = struct.calcsize(format)
tup = struct.unpack(format, s[:prelen])
varstr = s[prelen, -1] # -1 chops off null byte
Terry J. Reedy
Terry Reedy wrote: "Sergey Dorofeev" <se****@fidoman.ru> wrote in message news:d9***********@gavrilo.mtu.ru...
I can use string.unpack if string in struct uses fixed amount of bytes.
I presume you mean struct.unpack(format, string). The string len must be known when you call, but need not be fixed across multiple calls with different strings.
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.
Size is easy to determine. Given the above and string s (untested code): prelen = struct.calcsize('2l') strlen = s.find('\0', prelen) - prelen format = '2l %ds h c 3h' % strlen # c swallows null byte
shouldn't this be '2l %ds c 3h'?? Note that C structs can have only one variable-sized field and only at the end. With that restriction, one could slice and unpack the fixed stuff and then directly slice out the end string. (Again, untested)
format = 2l 3h' # for instance prelen = struct.calcsize(format) tup = struct.unpack(format, s[:prelen]) varstr = s[prelen, -1] # -1 chops off null byte
Perhaps you meant varstr = s[prelen:-1] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Joshua Forgione |
last post by:
Hello. I am reverse engineering some Python Code. I am using the book
"The Quick Python Book" as an aide.
The following line exists in the code:
msg = struct.pack('BBBB',word0, word1, word2,...
|
by: Panos Laganakos |
last post by:
Hello,
I have a list that includes lists of integers, in the form of:
li = , , ...]
packed = struct.pack(str(len(li)*3)+'i', li)
The frmt part is right, as I'm multiplying by 3, 'cause each...
|
by: tkpmep |
last post by:
I write data to Excel files using PyExcelerator 0.6.3.a and have done
so successfully for small files (10-15 cells). I'm experiencing an
error when writing a big chunk of data (10,000 cells) to...
|
by: Jansson Christer |
last post by:
Hi all,
I have discovered that in my Python 2.4.1 installation (on Solaris 8),
struct.pack handles things in a way that seems inconsistent to me.
I haven't found any comprehensible...
|
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???
:(
--
| |
by: aurora00 |
last post by:
I have a program that generates a number of files that will be
packaged into a tarball. Can I stream the content into TarFile without
first writing them out to the file system? All add(), addfile()...
|
by: praveen0437 |
last post by:
hi
i know about format argument in pack and unpack like b h i etc but in some cases i am getting < or = symbols along with them what does they signifies
for example struct.pack( "<cI", 'n',...
|
by: zhangbo2050 |
last post by:
Basically i'm trying to form a mac packet.
1. a simple mac header is generated using struct.pack('!HH', desti_mac, src_mac)
2. the packet is created using ''.join(header, .....)
Could anyone...
|
by: Steven Clark |
last post by:
Can anyone explain to me why
struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2)
gives 4 bytes?
-Steven
|
by: Cameron Simpson |
last post by:
On 25Jun2008 22:38, Steven Clark <steven.p.clark@gmail.comwrote:
| On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sjmachin@lexicon.netwrote:
| On Jun 26, 9:00 am, "Steven Clark"...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
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...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |