Want to build a binary header block

Discussion in 'Python' started by Bob Greschke, May 1, 2007.

  1. Bob Greschke

    Bob Greschke Guest

    This is the idea

    Block = pack("240s", "")
    Block[0:4] = pack(">H", W)
    Block[4:8] = pack(">H", X)
    Block[8:12] = pack(">B", Y)
    Block[12:16] = pack(">H", Z))

    but, of course, Block, a str, can't be sliced. The real use of this is a
    bit more complicated such that I can't just fill in all of the "fields"
    within Block in one giant pack(). I need to build it on the fly. For
    example the pack(">H", X) may be completely skipped some times through the
    function. There are many more fields in Block than in this example and they
    are all kinds of types not just H's and B's. What I really want is a C
    struct union. :)

    How would I do this?

    Thanks!

    Bob
     
    Bob Greschke, May 1, 2007
    #1
    1. Advertising

  2. On Tue, 1 May 2007 00:18:12 -0600, "Bob Greschke" <>
    declaimed the following in comp.lang.python:

    > This is the idea
    >
    > Block = pack("240s", "")
    > Block[0:4] = pack(">H", W)
    > Block[4:8] = pack(">H", X)
    > Block[8:12] = pack(">B", Y)
    > Block[12:16] = pack(">H", Z))
    >
    > but, of course, Block, a str, can't be sliced. The real use of this is a
    > bit more complicated such that I can't just fill in all of the "fields"
    > within Block in one giant pack(). I need to build it on the fly. For
    > example the pack(">H", X) may be completely skipped some times through the
    > function. There are many more fields in Block than in this example and they
    > are all kinds of types not just H's and B's. What I really want is a C
    > struct union. :)
    >
    > How would I do this?
    >

    Collect the parts in a list, and then

    "".join(blockList)

    (Empty quotes)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, May 1, 2007
    #2
    1. Advertising

  3. Bob Greschke

    Larry Bates Guest

    Bob Greschke wrote:
    > This is the idea
    >
    > Block = pack("240s", "")
    > Block[0:4] = pack(">H", W)
    > Block[4:8] = pack(">H", X)
    > Block[8:12] = pack(">B", Y)
    > Block[12:16] = pack(">H", Z))
    >
    > but, of course, Block, a str, can't be sliced. The real use of this is a
    > bit more complicated such that I can't just fill in all of the "fields"
    > within Block in one giant pack(). I need to build it on the fly. For
    > example the pack(">H", X) may be completely skipped some times through the
    > function. There are many more fields in Block than in this example and they
    > are all kinds of types not just H's and B's. What I really want is a C
    > struct union. :)
    >
    > How would I do this?
    >
    > Thanks!
    >
    > Bob
    >
    >

    When I have something like this I normally write a class
    for it and make its __str__ method return the packed output.

    Example (not tested, but you should get the drift):

    import struct

    class blockStruct:
    def __init__(self):
    self.hstring=240*" "
    self.W=None
    self.X=None
    self.Y=None
    self.Z=None
    return

    def __str__(self):
    rtnvals=[]
    rtnvals.append(struct.pack("240s", self.hstring)
    rtnvals.append(struct.pack(">H", W))
    .
    .
    .
    return ''.join(rtnvals)


    Then in your main program

    bS=blockStruct()
    bs.hstring='this is a test'.ljust(240, ' ')
    bs.W=12
    bs.X=17
    bs.Y=1
    bs.Z=0

    print bS

    Seemed to be a good way that made debugging and understanding
    easy for me.

    -Larry
     
    Larry Bates, May 1, 2007
    #3
  4. Bob Greschke <> wrote:
    > This is the idea
    >
    > Block = pack("240s", "")
    > Block[0:4] = pack(">H", W)
    > Block[4:8] = pack(">H", X)
    > Block[8:12] = pack(">B", Y)
    > Block[12:16] = pack(">H", Z))
    >
    > but, of course, Block, a str, can't be sliced.


    You could do this (note anonymous mappings require python 2.5)

    >>> from mmap import mmap
    >>> from struct import pack
    >>> W,X,Y,Z=1,2,3,4
    >>> Block = mmap(-1, 1024)
    >>> Block[0:2] = pack(">H", W)
    >>> Block[4:6] = pack(">H", X)
    >>> Block[8:9] = pack(">B", Y)
    >>> Block[12:14] = pack(">H", Z)
    >>> Block[0:16]

    '\x00\x01\x00\x00\x00\x02\x00\x00\x03\x00\x00\x00\x00\x04\x00\x00'
    >>>


    You might also want to consider Construct

    http://construct.wikispaces.com/

    From the web page: Construct is a python library for parsing and
    building of data structures (binary or textual). It is based on the
    concept of defining data structures in a declarative manner, rather
    than procedural code: more complex constructs are composed of a
    hierarchy of simpler ones. It's the first library that makes parsing
    fun, instead of the usual headache it is today.

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, May 2, 2007
    #4
    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. morrell
    Replies:
    1
    Views:
    1,005
    roy axenov
    Oct 10, 2006
  2. mlt
    Replies:
    2
    Views:
    914
    Jean-Marc Bourguet
    Jan 31, 2009
  3. Pilotmi80
    Replies:
    4
    Views:
    255
    Pilotmi80
    Jul 15, 2005
  4. Replies:
    4
    Views:
    181
    Ted Zlatanov
    Sep 13, 2006
  5. pavi
    Replies:
    0
    Views:
    1,395
Loading...

Share This Page