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

Want to build a binary header block

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


P: n/a
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))
May 1 '07 #2

P: n/a
Bob Greschke <bo*@greschke.comwrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
May 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.