R
Roy Smith
I need to generate packed binary data which includes null-terminated
strings mixed in with binary numbers. For example, the Python string
"foo" needs to become the 4-character binary string "foo\0", just like a
C string (don't ask, I didn't design the protocol).
What's the best way to do this? The "s" format specifier in struct
gives me fixed-length, non-null-terminated strings. Right now I'm doing:
s1 = self.directory + "\0"
s2 = self.fileName + "\0"
s3 = self.dateString + "\0"
s4 = self.name + "\0"
s5 = struct.pack ("!IB", self.id, self.perTargetFlag)
data = s1 + s2 + s3 + s4 + s5
which is pretty ugly. Is there a neater way to do this?
I know the string addition isn't the most efficient way to go, but for
this application, I prefer the clarity and don't mind the minor speed
hit.
strings mixed in with binary numbers. For example, the Python string
"foo" needs to become the 4-character binary string "foo\0", just like a
C string (don't ask, I didn't design the protocol).
What's the best way to do this? The "s" format specifier in struct
gives me fixed-length, non-null-terminated strings. Right now I'm doing:
s1 = self.directory + "\0"
s2 = self.fileName + "\0"
s3 = self.dateString + "\0"
s4 = self.name + "\0"
s5 = struct.pack ("!IB", self.id, self.perTargetFlag)
data = s1 + s2 + s3 + s4 + s5
which is pretty ugly. Is there a neater way to do this?
I know the string addition isn't the most efficient way to go, but for
this application, I prefer the clarity and don't mind the minor speed
hit.