[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Binary file Pt 1 - Only reading some

Mastastealth

2/5/2008 3:51:00 AM

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.
5 Answers

Gabriel Genellina

2/5/2008 6:17:00 AM

0

On 5 feb, 01:51, Mastastealth <mastastea...@gmail.com> wrote:
> 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-s...

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

Mastastealth

2/5/2008 1:50:00 PM

0

On Feb 5, 1:17 am, Gabriel Genellina <gagsl-...@yahoo.com.ar> wrote:
> Using the struct module  http://docs.python.org/lib/module-s...
>
> 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.

Mastastealth

2/5/2008 3:49:00 PM

0

On Feb 5, 8:50 am, Mastastealth <mastastea...@gmail.com> wrote:
> 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

Gabriel Genellina

2/5/2008 5:54:00 PM

0

En Tue, 05 Feb 2008 11:50:25 -0200, Mastastealth <mastastealth@gmail.com>
escribi�:

> On Feb 5, 1:17 am, Gabriel Genellina <gagsl-...@yahoo.com.ar> wrote:
>> Using the struct module  http://docs.python.org/lib/module-s...
>>
>> 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.wiki...

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

--
Gabriel Genellina

Jason

1/19/2013 6:16:00 AM

0

In article <uhmjf8lmjdsp5agun9k5lapskalckhgifq@4ax.com>, Alan Ferris
<hairy.ferrit@yahoo.co.uk> wrote:

> On Fri, 18 Jan 2013 13:38:28 -0800, Jason@nospam.com (Jason) wrote:
>
> >In article <4sqif8d0560rtolnkuuukprsjm10p69ffb@4ax.com>, Alan Ferris
> ><hairy.ferrit@yahoo.co.uk> wrote:
> >
> >> On Thu, 17 Jan 2013 16:22:56 -0800, Jason@nospam.com (Jason) wrote:
> >>
> >> >In article <6o1hf8dop4nmi5tq3j4tace1c60hdcc4of@4ax.com>, Alan Ferris
> >> ><hairy.ferrit@yahoo.co.uk> wrote:
> >> >
> >> >> On Thu, 17 Jan 2013 14:49:44 -0800, Jason@nospam.com (Jason) wrote:
> >> >>
> >> >> >In article <rmjgf8dm43t0gu19mhtb24k42e0sbq24c1@4ax.com>, Alan Ferris
> >> >> ><hairy.ferrit@yahoo.co.uk> wrote:
> >> >> >
> >> >> >> On Wed, 16 Jan 2013 14:11:42 -0800, Jason@nospam.com (Jason) wrote:
> >> >> >>
> >> >> >> >In article <3i6ef89halkup46mhi9t4coadgib7jls7v@4ax.com>, Alan Ferris
> >> >> >> ><hairy.ferrit@yahoo.co.uk> wrote:
> >> >> >> >
> >> >> >> >> On Wed, 16 Jan 2013 13:13:56 -0800, Jason@nospam.com (Jason)
wrote:
> >> >> >> >>
> >> >> >> >> >> >The subject came up once before in relation to a book
that I read
> >> >> >about a
> >> >> >> >> >> >dozen years ago. The book discussed theories about what heaven
> >> >> >was like.
> >> >> >> >> >> >
> >> >> >> >> >> Which book.
> >> >> >> >> >
> >> >> >> >> >It may have been "Glimpes of Eternity" by Raymond A. Moody, Jr.
> >> >> >> >>
> >> >> >> >> So 12 years ago you read a book published only 2 years ago.
> >> >> >Brilliant. Now
> >> >> >> >> you have proved once again to people what a dumb liar you are.
> >> >> >>
> >> >> >> >
> >> >> >> >You missed the first 4 words of my above sentence. I read a
> >similar book
> >> >> >> >many years ago. It may have been written by Raymond A. Moody, Jr.
> >I don't
> >> >> >> >remember.
> >> >> >> >
> >> >> >> "The subject came up" Where does this say similar book?
> >> >> >>
> >> >> >> You claimed to have read the book 12 years ago:
> >> >> >>
> >> >> >> "book that I read about a dozen years ago."
> >> >> >>
> >> >> >> And when you were asked for the name of the book you produce one
> >> >published 2
> >> >> >> years ago.
> >> >> >>
> >> >> >> So it was an outright lie. A very stupid lie.
> >> >> >
> >> >> >Please note that you left out "It may have been..."
> >> >> >
> >> >> Well DUH it clearly wasn't. 2 years to 12 years showed you clearly were
> >> >making
> >> >> it up. Try again.
> >> >
> >> >I don't remember when I read it and the title of the book.
> >> >
> >> So this was a blatant lie then:
> >>
> >> "The subject came up once before in relation to a book that I read about a
> >> dozen years ago"
>
> >
> >I said about a dozen years ago. I used the word "about" since I did not
> >know the exact number of years.
> >
> ROFLAMO!
>
> So you thin 12 years is about 2 years ago.
>
> Do you realise how thick you have just made yourself look?
> --
> Ferrit
>
> ()'.'.'()
> ( (T) )
> ( ) . ( )
> (")_(")
> Atheist #1211
> EAC(UK)#252 Ironic Torture Div.
>
> SERV:
> http://www.youtube.com/watch?v=W...
> https://www.youtube.com/watch?v=D...
> https://www.youtube.com/watch?v=6...
>
>
>

It's the plight of people that have serious memory problems to not
remember where they have read or heard information. For example, if
someone walks up to me and knows my name and I don't have a clue about
where I seen him or her before--it is very frustrating.