473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Binary file Pt 1 - Only reading some

I'm trying to create a program to read a certain binary format. I have
the format's spec which goes something like:

First 6 bytes: String
Next 4 bytes: 3 digit number and a blank byte
---
Next byte: Height (Number up to 255)
Next byte: Width (Number up to 255)
Next byte: Number 0 - 5
Every 2 bytes after that: Supposedly a number 0000 - 0899?

Anyway, I'm able to do the first 2 objects fine:

a = info.read(6)
b = info.read(4)

Printing both gives me what I mentioned above, a string and a 3 digit
number with a space. However, as I continue, things get trickier.

c = info.read(1)
d = info.read(1)

Printing c and d in this case gives me a block in the SPE output, or
if I run in a DOS prompt, 2 funny symbols. How do I get an integer out
of this? I'll probably need help once I get to the "every 2 byte"
section, but that'll be a separate post.
Feb 5 '08 #1
4 1468
On 5 feb, 01:51, Mastastealth <mastastea...@gmail.comwrote:
I'm trying to create a program to read a certain binary format. I have
the format's spec which goes something like:

First 6 bytes: String
Next 4 bytes: 3 digit number and a blank byte
---
Next byte: Height (Number up to 255)
Next byte: Width (Number up to 255)
Next byte: Number 0 - 5
Every 2 bytes after that: Supposedly a number 0000 - 0899?

Anyway, I'm able to do the first 2 objects fine:

a = info.read(6)
b = info.read(4)

Printing both gives me what I mentioned above, a string and a 3 digit
number with a space. However, as I continue, things get trickier.

c = info.read(1)
d = info.read(1)

Printing c and d in this case gives me a block in the SPE output, or
if I run in a DOS prompt, 2 funny symbols. How do I get an integer out
of this? I'll probably need help once I get to the "every 2 byte"
section, but that'll be a separate post.
Using the struct module http://docs.python.org/lib/module-struct.html

import struct
data = info.read(15)
str1, str2, blank, height, width, num2, num3 =
struct.unpack("6s3s1cBBBh", data)

Consider this like a "first attempt", open issues: is the data little-
endian or big-endian? does the 0-5 mean 0x00-0x05 or "0"-"5"? the last
numbers are 2-byte binary integers, or 0000-0899 might indicate BDC?
But building the right format is surely faster and easier than parsing
the data by hand.

--
Gabriel Genellina
Feb 5 '08 #2
On Feb 5, 1:17*am, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
Using the struct module *http://docs.python.org/lib/module-struct.html

import struct
data = info.read(15)
str1, str2, blank, height, width, num2, num3 =
struct.unpack("6s3s1cBBBh", data)

Consider this like a "first attempt", open issues: is the data little-
endian or big-endian? does the 0-5 mean 0x00-0x05 or "0"-"5"? the last
numbers are 2-byte binary integers, or 0000-0899 might indicate BDC?
But building the right format is surely faster and easier than parsing
the data by hand.

--
Gabriel Genellina
Ah ok, thanks! That worked, though the line "str1, str2, blank,
height, width, num2, num3 =" spit out a syntax error. However, I do
see that it creates a tuple with all the values in a readable format
for me. Also, I needed to change info.read(15) to 16. More questions:

What is this value for? "6s3s1cBBBh" and why is my unpack limited to a
length of "16"?

Unfortunately it seems my understanding of binary is way too basic for
what I'm dealing with. Can you point me to a simple guide to
explaining most of it? As far as I know this is just a bunch of 1's
and 0's right? Each byte has 8 digits of, of which somehow is
converted to a number or letter. Don't know what most of that stuff in
the struct page means. -_-

As for you questions, I suppose it would be "little-endian" as the
format is on PC (and the Python docs say: "Intel and DEC processors
are little-endian"). 0-5 means a single digit "0" through "5". Lastly,
I'm not building the format, it's already made (a format for tiled
maps in a game). My program is just reading it.
Feb 5 '08 #3
On Feb 5, 8:50*am, Mastastealth <mastastea...@gmail.comwrote:
What is this value for? "6s3s1cBBBh" and why is my unpack limited to a
length of "16"?

Unfortunately it seems my understanding of binary is way too basic for
what I'm dealing with. Can you point me to a simple guide to
explaining most of it? As far as I know this is just a bunch of 1's
and 0's right? Each byte has 8 digits of, of which somehow is
converted to a number or letter. Don't know what most of that stuff in
the struct page means. -_-
Ah never mind, after closer inspection is was something on my side of
the code that ruined the tuple unpacking. Also, I now understand (a
little) what the string meant.

6s = A string of 6 characters
3s = String with 3 characters
1c = For the blank character
B's = An unsigned char, any number I guess?
h = short integer. Whatever that is.

Either way, just answering myself so you don't have to explain it. :D
Feb 5 '08 #4
En Tue, 05 Feb 2008 11:50:25 -0200, Mastastealth <ma**********@gmail.com>
escribi�:
On Feb 5, 1:17Â*am, Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
>Using the struct module Â*http://docs.python.org/lib/module-struct.html

import struct
data = info.read(15)
str1, str2, blank, height, width, num2, num3 =
struct.unpack("6s3s1cBBBh", data)

Consider this like a "first attempt", open issues: is the data little-
endian or big-endian? does the 0-5 mean 0x00-0x05 or "0"-"5"? the last
numbers are 2-byte binary integers, or 0000-0899 might indicate BDC?
But building the right format is surely faster and easier than parsing
the data by hand.

Ah ok, thanks! That worked, though the line "str1, str2, blank,
height, width, num2, num3 =" spit out a syntax error. However, I do
see that it creates a tuple with all the values in a readable format
for me. Also, I needed to change info.read(15) to 16. More questions:

What is this value for? "6s3s1cBBBh" and why is my unpack limited to a
length of "16"?
That's the format describing how to decode your bytes: first, a string of
six characters; then a string of 3 characters followed by a single
character, three individual bytes, and a short integer.
Unfortunately it seems my understanding of binary is way too basic for
what I'm dealing with. Can you point me to a simple guide to
explaining most of it? As far as I know this is just a bunch of 1's
and 0's right? Each byte has 8 digits of, of which somehow is
converted to a number or letter. Don't know what most of that stuff in
the struct page means. -_-
I'm afraid I don't know any simple guide to struct. Perhaps people think
that if you manage binary data with struct, you must already be a Python
guru or something...
You could try "construct" instead: "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. [...] It's the first library that makes parsing fun,
instead of the usual headache it is today."
http://pyconstruct.wikispaces.com

There are some recipes in the Python Cookbook too:
http://aspn.activestate.com/ASPN/Cookbook/Python

--
Gabriel Genellina

Feb 5 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.