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 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))
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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....
|
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....
| |
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...
|
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...
|
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 =...
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |