Hello,
I am having trouble writing the code to read a binary string. I would
like to extract the values for use in a calculation.
Any help would be great.
Here is my function that takes in a string.
def parseSequence(d ata, start):
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
pos = start+8+length
element = (group_num+elem ent_num)
if element == '\xfe\xff\x00\x e0':
data = value
while start < length:
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
start = start+8+length
element = (group_num+elem ent_num)
if element == '\xfe\xff\x00\x e0':
data = value
while start < length:
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
start = start+8+length
element = (group_num+elem ent_num)
return element, start, value
else:
return element, start, value
else:
return element, pos, value
And, here is a sample string (I have split up and indented for
readability). There is an identifier (\xfe\xff\x00\x e0) followed by
the length of the nested values.
'\xfe\xff\x00\x e0\x18\x02\x00\ x00 -length=536
\n0q\x00\x02\x0 0\x00\x001
\n0x\x00\x02\x0 0\x00\x0010
\n0\x80\x00\x02 \x00\x00\x004
\n0\xa0\x00\x02 \x00\x00\x000
\x0c0\x04\x00\x e8\x01\x00\x00
\xfe\xff\x00\xe 0p\x00\x00\x00 -length=112
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x0c \x00\x00\x008.9 617062e-1
\n0\x86\x00\x10 \x00\x00\x00127 .378510918301
\x0c0\x06\x00\x 02\x00\x00\x001
\xfe\xff\x00\xe 0p\x00\x00\x00 -length=112
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x0c \x00\x00\x001.6 29998e-1
\n0\x86\x00\x10 \x00\x00\x0023. 159729257873
\x0c0\x06\x00\x 02\x00\x00\x004
\xfe\xff\x00\xe 0t\x00\x00\x00 -length=116
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x10 \x00\x00\x001.2 6285318894435
\n0\x86\x00\x10 \x00\x00\x00227 .690980638769
\x0c0\x06\x00\x 02\x00\x00\x003
\xfe\xff\x00\xe 0t\x00\x00\x00 -length=116
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x10 \x00\x00\x001.5 2797639111557
\n0\x86\x00\x10 \x00\x00\x00263 .433384670643
\x0c0\x06\x00\x 02\x00\x00\x002 ') 8 1937 Br***********@g mail.com wrote:
Hello,
I am having trouble writing the code to read a binary string. I would
like to extract the values for use in a calculation.
Any help would be great.
Without having looked at your code an any detail, may I humbly suggest
that you throw it all out and use the struct module: http://docs.python.org/lib/module-struct.html
It is meant to solve this kind of problem, and it is quite easy to use.
Gary Herron
Here is my function that takes in a string.
def parseSequence(d ata, start):
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
pos = start+8+length
element = (group_num+elem ent_num)
if element == '\xfe\xff\x00\x e0':
data = value
while start < length:
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
start = start+8+length
element = (group_num+elem ent_num)
if element == '\xfe\xff\x00\x e0':
data = value
while start < length:
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
start = start+8+length
element = (group_num+elem ent_num)
return element, start, value
else:
return element, start, value
else:
return element, pos, value
And, here is a sample string (I have split up and indented for
readability). There is an identifier (\xfe\xff\x00\x e0) followed by
the length of the nested values.
'\xfe\xff\x00\x e0\x18\x02\x00\ x00 -length=536
\n0q\x00\x02\x0 0\x00\x001
\n0x\x00\x02\x0 0\x00\x0010
\n0\x80\x00\x02 \x00\x00\x004
\n0\xa0\x00\x02 \x00\x00\x000
\x0c0\x04\x00\x e8\x01\x00\x00
\xfe\xff\x00\xe 0p\x00\x00\x00 -length=112
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x0c \x00\x00\x008.9 617062e-1
\n0\x86\x00\x10 \x00\x00\x00127 .378510918301
\x0c0\x06\x00\x 02\x00\x00\x001
\xfe\xff\x00\xe 0p\x00\x00\x00 -length=112
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x0c \x00\x00\x001.6 29998e-1
\n0\x86\x00\x10 \x00\x00\x0023. 159729257873
\x0c0\x06\x00\x 02\x00\x00\x004
\xfe\xff\x00\xe 0t\x00\x00\x00 -length=116
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x10 \x00\x00\x001.2 6285318894435
\n0\x86\x00\x10 \x00\x00\x00227 .690980638769
\x0c0\x06\x00\x 02\x00\x00\x003
\xfe\xff\x00\xe 0t\x00\x00\x00 -length=116
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x10 \x00\x00\x001.5 2797639111557
\n0\x86\x00\x10 \x00\x00\x00263 .433384670643
\x0c0\x06\x00\x 02\x00\x00\x002 ')
On Mar 29, 3:44 pm, "Bryan.Fodn...@ gmail.com"
<Bryan.Fodn...@ gmail.comwrote:
Hello,
I am having trouble writing the code to read a binary string. I would
like to extract the values for use in a calculation.
Any help would be great.
I'm too lazy to debug your binary string, but I suggest that you
completely throw away the binary file and restart with a database or
structured text. See, for example: http://pyyaml.org/wiki/PyYAML
If you have some legacy binary file that you need to process, try
creating a C program that freads the binary file and printfs a text
equivalent.
If the decision of using binary files is not yours, then
>
Here is my function that takes in a string.
def parseSequence(d ata, start):
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
pos = start+8+length
element = (group_num+elem ent_num)
if element == '\xfe\xff\x00\x e0':
data = value
while start < length:
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
start = start+8+length
element = (group_num+elem ent_num)
if element == '\xfe\xff\x00\x e0':
data = value
while start < length:
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack(' hh', vl_field)[0]
value = data[start+8:(start+ 8+length)]
start = start+8+length
element = (group_num+elem ent_num)
return element, start, value
else:
return element, start, value
else:
return element, pos, value
And, here is a sample string (I have split up and indented for
readability). There is an identifier (\xfe\xff\x00\x e0) followed by
the length of the nested values.
'\xfe\xff\x00\x e0\x18\x02\x00\ x00 -length=536
\n0q\x00\x02\x0 0\x00\x001
\n0x\x00\x02\x0 0\x00\x0010
\n0\x80\x00\x02 \x00\x00\x004
\n0\xa0\x00\x02 \x00\x00\x000
\x0c0\x04\x00\x e8\x01\x00\x00
\xfe\xff\x00\xe 0p\x00\x00\x00 -length=112
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x0c \x00\x00\x008.9 617062e-1
\n0\x86\x00\x10 \x00\x00\x00127 .378510918301
\x0c0\x06\x00\x 02\x00\x00\x001
\xfe\xff\x00\xe 0p\x00\x00\x00 -length=112
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x0c \x00\x00\x001.6 29998e-1
\n0\x86\x00\x10 \x00\x00\x0023. 159729257873
\x0c0\x06\x00\x 02\x00\x00\x004
\xfe\xff\x00\xe 0t\x00\x00\x00 -length=116
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x10 \x00\x00\x001.2 6285318894435
\n0\x86\x00\x10 \x00\x00\x00227 .690980638769
\x0c0\x06\x00\x 02\x00\x00\x003
\xfe\xff\x00\xe 0t\x00\x00\x00 -length=116
\n0\x82\x002\x0 0\x00\x0042.906 8704277562\\-392.3545926477\
\189.1821120994 44
\n0\x84\x00\x10 \x00\x00\x001.5 2797639111557
\n0\x86\x00\x10 \x00\x00\x00263 .433384670643
\x0c0\x06\x00\x 02\x00\x00\x002 ')
On Mar 30, 3:58 pm, hdante <hda...@gmail.c omwrote:
On Mar 29, 3:44 pm, "Bryan.Fodn...@ gmail.com"
<Bryan.Fodn...@ gmail.comwrote:
Hello,
I am having trouble writing the code to read a binary string. I would
like to extract the values for use in a calculation.
Any help would be great.
I'm too lazy to debug your binary string, but I suggest that you
completely throw away the binary file and restart with a database or
structured text. See, for example:
http://pyyaml.org/wiki/PyYAML
If you have some legacy binary file that you need to process, try
creating a C program that freads the binary file and printfs a text
equivalent.
.... and that couldn't be done faster and better in Python??
On Mar 30, 4:31 am, John Machin <sjmac...@lexic on.netwrote:
On Mar 30, 3:58 pm, hdante <hda...@gmail.c omwrote:
On Mar 29, 3:44 pm, "Bryan.Fodn...@ gmail.com"
<Bryan.Fodn...@ gmail.comwrote:
Hello,
I am having trouble writing the code to read a binary string. I would
like to extract the values for use in a calculation.
Any help would be great.
I'm too lazy to debug your binary string, but I suggest that you
completely throw away the binary file and restart with a database or
structured text. See, for example:
http://pyyaml.org/wiki/PyYAML
If you have some legacy binary file that you need to process, try
creating a C program that freads the binary file and printfs a text
equivalent.
... and that couldn't be done faster and better in Python??
No. A C struct is done faster and better than python (thus, the
correctness check is faster in C). Also, chances are high that there's
already an include file with the binary structure.
hdante schrieb:
On Mar 30, 4:31 am, John Machin <sjmac...@lexic on.netwrote:
>On Mar 30, 3:58 pm, hdante <hda...@gmail.c omwrote:
>>On Mar 29, 3:44 pm, "Bryan.Fodn...@ gmail.com" <Bryan.Fodn.. .@gmail.comwrot e: Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. I'm too lazy to debug your binary string, but I suggest that you completely throw away the binary file and restart with a database or structured text. See, for example: http://pyyaml.org/wiki/PyYAML If you have some legacy binary file that you need to process, try creating a C program that freads the binary file and printfs a text equivalent.
... and that couldn't be done faster and better in Python??
No. A C struct is done faster and better than python (thus, the
correctness check is faster in C). Also, chances are high that there's
already an include file with the binary structure.
That is utter nonsense. There is no "correctnes s check" in C. and using
printf & thus creating strings that you then need to parse in python
just doubles the effort needlessly.
The standard-lib module "struct" is exactly what you need, nothing else.
it sure is faster than any parsing of preprocessed data, doesn't
introduce a language-mixture and is prototyped/tested much faster
because of it being python - and not C-compiler and C-debugger.
Alternatively, *IF* there were C-structure-declarations available for
the binary format, the usage of ctypes would allow for roughly the same,
even reducing the effort to create the structure definition a great deal.
Diez
On Mar 30, 9:23 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
hdante schrieb:
On Mar 30, 4:31 am, John Machin <sjmac...@lexic on.netwrote:
On Mar 30, 3:58 pm, hdante <hda...@gmail.c omwrote:
>On Mar 29, 3:44 pm, "Bryan.Fodn...@ gmail.com" <Bryan.Fodn... @gmail.comwrote : Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. I'm too lazy to debug your binary string, but I suggest that you completely throw away the binary file and restart with a database or structured text. See, for example: http://pyyaml.org/wiki/PyYAML If you have some legacy binary file that you need to process, try creating a C program that freads the binary file and printfs a text equivalent.
... and that couldn't be done faster and better in Python??
No. A C struct is done faster and better than python (thus, the
correctness check is faster in C). Also, chances are high that there's
already an include file with the binary structure.
That is utter nonsense. There is no "correctnes s check" in C. and using
printf & thus creating strings that you then need to parse in python
just doubles the effort needlessly.
The standard-lib module "struct" is exactly what you need, nothing else.
it sure is faster than any parsing of preprocessed data, doesn't
introduce a language-mixture and is prototyped/tested much faster
because of it being python - and not C-compiler and C-debugger.
Alternatively, *IF* there were C-structure-declarations available for
the binary format, the usage of ctypes would allow for roughly the same,
even reducing the effort to create the structure definition a great deal.
Diez
Whatever you say.
>
Whatever you say.
Can't express what your approval means to me!
Diez
On Sun, 30 Mar 2008 04:48:59 -0700 (PDT), hdante <hd****@gmail.c omwrote:
On Mar 30, 4:31 am, John Machin <sjmac...@lexic on.netwrote:
>On Mar 30, 3:58 pm, hdante <hda...@gmail.c omwrote:
If you have some legacy binary file that you need to process, try
creating a C program that freads the binary file and printfs a text
equivalent.
... and that couldn't be done faster and better in Python??
No. A C struct is done faster and better than python (thus, the
correctness check is faster in C). Also, chances are high that there's
already an include file with the binary structure.
If a C struct defines the file format, he is probably screwed already.
There are no guarantees that even different compilers on the same
machine have the same struct layout.
I have never seen this done by a serious program.
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brandon McCombs |
last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm
trying here.
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat", ios::in | ios::out | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created. The only time it is created
is when I specify ifstream or ofstream but not fstream. I've tried
removing the...
|
by: |
last post by:
I am rewriting a C++ application in C#. This file has a combination of Text
and Binary data.
I used CFile before to read the text. If I hit a certain string that
denotes the following data is binary, I used the current position in the
file and another stream to read to the binary data.
All text data is ended with a carriage return / line feed while the binary
is actually an image file listed byte by byte. Preceding the binary data...
|
by: ishmael4 |
last post by:
hello everyone!
i have a problem with reading from binary file. i was googling and
searching, but i just cant understand, why isnt this code working. i could
use any help. here's the source code:
--cut here--
typedef struct pkg_ {
short int info;
char* data;
|
by: Abhishek |
last post by:
I have a problem transfering files using sockets from pocket pc(.net compact
c#) to desktop(not using .net just mfc and sockets 2 API). The socket
communication is not a issue and I am able to transfer data across.On the
serve I am using Socket 2 API (recv function to read bytes)and not using
..NET. I use FileStream to open the file on the pocket pc, then associate a
BinaryReader object with the stream and call ReadBytes to read all the...
|
by: Mad Scientist Jr |
last post by:
i'm trying to read a file byte by byte (and later alter the data and
write it to a 2nd file byte by byte) and running into a problem where
it seems to keep reading the same byte over and over again (an endless
loop). i thought that BinaryReader.ReadByte advanced to the next byte?
i had it time out after 1000 iterations, and keeps outputting the same
byte. any help appreciated, my code is below:
Imports System.io
| |
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: efrenba |
last post by:
Hi,
I came from delphi world and now I'm doing my first steps in C++. I'm
using C++builder because its ide is like delphi although I'm trying to
avoid the vcl.
I need to insert new features to an old program that I wrote in delphi
and it's a good opportunity to start with c++.
|
by: Neil Crighton |
last post by:
I'm using the zipfile library to read a zip file in Windows, and it
seems to be adding too many newlines to extracted files. I've found
that for extracted text-encoded files, removing all instances of '\r'
in the extracted file seems to fix the problem, but I can't find an
easy solution for binary files.
The code I'm using is something like:
from zipfile import Zipfile
z = Zipfile(open('zippedfile.zip'))
|
by: Jeff |
last post by:
Im trying to figure out why I cant read back a binary file correctly.
I have the following union:
#define BITE_RECORD_LEN 12
typedef union {
unsigned char byte;
struct {
unsigned char type; /* 0 Type */
unsigned char sn; /* 1-3 Serial Number */
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |