Want to build a binary header block

B

Bob Greschke

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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Larry Bates

Bob said:
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
 
N

Nick Craig-Wood

Bob Greschke said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top