Null-terminated strings with struct module?

Discussion in 'Python' started by Roy Smith, Mar 6, 2004.

  1. Roy Smith

    Roy Smith Guest

    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.
     
    Roy Smith, Mar 6, 2004
    #1
    1. Advertising

  2. Roy Smith

    Paul Rubin Guest

    Roy Smith <> writes:
    > 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?


    Shrug.

    data = "%s\0%s\0%s\0%s\0%s"% (self.directory, self.fileName,
    self.dateString, self.name,
    struct.pack (...))

    Dunno if that's better or worse.
     
    Paul Rubin, Mar 6, 2004
    #2
    1. Advertising

  3. Roy Smith

    Peter Otten Guest

    Roy Smith wrote:

    > 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.


    Does that rule out the following?

    data = "\0".join([self.directory, self.fileName, ..., struct.pack(...)])

    Or perhaps you could mangle attribute names (untested):

    def __getattr__(self, name):
    flag, name = name[0], name[1].lower() + name[2:]
    value = getattr(self, name)
    if flag == "Z":
    return value + "\0"
    else:
    return struct.pack("!" + flag, value)

    data = self.ZDirectory + ... + self.ZName + self.IId + self.BPerTargetFlag

    Peter
     
    Peter Otten, Mar 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    478
    Roedy Green
    Jul 9, 2003
  2. Barry

    strncpy() and null terminated strings

    Barry, Apr 8, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,171
    Malcolm
    Apr 8, 2004
  3. Matt Helm
    Replies:
    5
    Views:
    1,617
    Matt Helm
    Feb 6, 2006
  4. Replies:
    12
    Views:
    734
  5. ssylee
    Replies:
    4
    Views:
    515
    CBFalconer
    Aug 12, 2008
Loading...

Share This Page