[Pythonmac-SIG] Read Strings from Resources

Just van Rossum just at letterror.com
Wed Dec 22 20:39:27 CET 2004


wicked witch wrote:

> >>>So it looks like a 2-byte count followed by an array of Pascal
> >>>strings.
> >>
> >>Ah yes. And now that I see this I seem to remember that the pstrings
> >>are padded to a 2-byte boundary.
> > 
> > Hmmm, I'd be a bit surprised if there was such alignment padding.
> > Pascal string lengths are one byte, so after the single integer
> > listed above everything else should just be byte-aligned.
> > 
> yeah you are right. Still it would be much easier with the
> GetIndString method. Thx for your help. I implemented it in c++ this
> time(was just a litle tool).

Here's a quick Python version:

  >>> import struct
  >>> def unpackStr(data):
  ...   result = []
  ...   n, = struct.unpack(">H", data[:2])
  ...   data = data[2:]
  ...   for i in range(n):
  ...     l = ord(data[0])
  ...     result.append(data[1:1+l])
  ...     data = data[1+l:]
  ...   return result 
  ... 
  >>> data = "\000\005\004abcd\000\002xy\01012345678\001z"
  >>> unpackStr(data)
  ['abcd', '', 'xy', '12345678', 'z']
  >>> 

Just


More information about the Pythonmac-SIG mailing list