By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

bit parsing from file

P: n/a
Hello,

I'm tring to make a cutting script.
The problem is the following, i have a sample pattern, for
example :'11101110' (0xEE)
That is the sign of the data begining.
How can i cut a file if the byte stepping is not the same, for
example:

file=open('test.bin','rb')
data=file.read()
print binascii.hexlify(data) # BB9A (1011101110011010)
file.close()

so i need to cut the first two bit and start to writeout the bit
stream to another file
If somebody have an idea to do this, please share with me.
Thanx

Rew
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 29, 9:42�am, "rewo...@gmail.com" <rewo...@gmail.comwrote:
Hello,

I'm tring to make a cutting script.
The problem is the following, i have a sample pattern, for
example :'11101110' (0xEE)
That is the sign of the data begining.
How can i cut a file if the byte stepping is not the same, for
example:

file=open('test.bin','rb')
data=file.read()
print binascii.hexlify(data) � � � � � �# BB9A (1011101110011010)
file.close()

so i need to cut the first two bit and start to writeout the bit
stream to another file
If somebody have an idea to do this, please share with me.
Thanx
Cutting off the leading two bits isn't a problem,
but once you write out the byte 11101110 you are
left with 011010 which cannot be written as it's not
a byte. How do you plan to handle that? Add two bits
at the MSB (00011010) or two bits at the LSB (01101000)
or discard the fractional byte?

And do you always know what the bit offset is or
do you have to search for the 11101110 starting
pattern?
>
Rew
Jun 27 '08 #2

P: n/a
On máj. 29, 18:26, Mensanator <mensana...@aol.comwrote:
On May 29, 9:42�am, "rewo...@gmail.com" <rewo...@gmail.comwrote:


Hello,
I'm tring to make a cutting script.
The problem is the following, i have a sample pattern, for
example :'11101110' (0xEE)
That is the sign of the data begining.
How can i cut a file if the byte stepping is not the same, for
example:
file=open('test.bin','rb')
data=file.read()
print binascii.hexlify(data) � � � � � �# BB9A (1011101110011010)
file.close()
so i need to cut the first two bit and start to writeout the bit
stream to another file
If somebody have an idea to do this, please share with me.
Thanx

Cutting off the leading two bits isn't a problem,
but once you write out the byte 11101110 *you are
left with 011010 which cannot be written as it's not
a byte. How do you plan to handle that? Add two bits
at the MSB (00011010) or two bits at the LSB (01101000)
or discard the fractional byte?

And do you always know what the bit offset is or
do you have to search for the 11101110 starting
pattern?
Thank's for the reply,
Yes, I have to search for the pattern, the bit offset not always the
same.
for another thing, it's ok if i can fill up with zero at the LSB or
discard that byte. (the last byte not important)

Rew
Jun 27 '08 #3

P: n/a
On May 30, 9:16*am, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Thu, 29 May 2008 09:37:54 -0700 (PDT), "rewo...@gmail.com"
<rewo...@gmail.comdeclaimed the following in comp.lang.python:
Thank's for the reply,
Yes, I have to search for the pattern, the bit offset not always the
same.
for another thing, it's ok if i can fill up with zero at the LSB or
discard that byte. (the last byte not important)

* * * * Sounds suspiciously like an HDLC protocol...

* * * * You'll likely need to create a library that can extract the properly
aligned bytes by holding one -- properly shifted -- and fed the next
byte.

* * * * In very loose pseudo-code

detect bit offset
initialize extractor with first valid byte and offset
for inbyte in input:
* * * * outbyte = extract_feed(inbyte)

where extract_feed() shifts the remainder (or first byte) left by the
offset (into a 16bit value), adds the new byte, ANDs with an 8-bit mask
with proper offset, returns left most result byte while also clearing
the remainder portion
--
* * * * Wulfraed * * * *Dennis Lee Bieber * * * * * * * KD6MOG
* * * * wlfr...@ix.netcom.com * * * * * * *wulfr...@bestiaria.com
* * * * * * * * HTTP://wlfraed.home.netcom.com/
* * * * (Bestiaria Support Staff: * * * * * * * web-a...@bestiaria.com)
* * * * * * * * HTTP://www.bestiaria.com/
Thank you for your help, i will try to do something like that.
I did some test about extracting byte with binascii, i will post it
tomorow.

Rew
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.