struct.(un)pack and ASCIIZ strrings

John Machin sjmachin at lexicon.net
Sat Jun 18 20:01:25 EDT 2005


Terry Reedy wrote:
> "Sergey Dorofeev" <sergey at fidoman.ru> wrote in message 
> news:d926sj$1jc8$1 at 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]




More information about the Python-list mailing list