473,473 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Want to build a binary header block

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
2 2019
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
10
by: Cam | last post by:
Hi all, The code below is a practical exercise and works well running in the debug environment but fails when being compiled for a release build. I believe this is because the debug environment...
2
by: peter.havens | last post by:
Hi all, I'm attempting to build Python 2.4.1 on Solaris 10 using gcc 3.4.3. I get the following build error: <snip> gcc -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
by: Ronin | last post by:
I'm very new to C++ coming in from using C# and the like. I've read a lot of good posts about binary files but I'm still missing a few key ingredients that will help me with the following problem....
3
by: showson1 | last post by:
Hi all. We have some files that are basically TIF images with a text header. The header portion is blocked by {startdb} and {enddb}.. it is the same format for all the files. What I need to do...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
2
by: Giovanni Gherdovich | last post by:
Hello, as you know the algorithm std::accumulate in the version template<class In, class T, class BinOp> T accumulate(In first, In last, T init, BinOp op) { while(first != last) init =...
4
by: M.-A. Lemburg | last post by:
Hi Robin, On 2008-10-23 17:55, Robin Becker wrote: That looks like a classical name clash between C header files. It also suggests that you have 64-bit client libs of MySQL installed. As...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.