By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

struct.(un)pack and ASCIIZ strrings

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"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

Jul 19 '05 #2

P: n/a
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]

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.