bit parsing from file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rewonka@gmail.com

    bit parsing from file

    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.hexlif y(data) # BB9A (10111011100110 10)
    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
  • Mensanator

    #2
    Re: bit parsing from file

    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.hexlif y(data) � � � � � �# BB9A (10111011100110 10)
    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

    Comment

    • rewonka@gmail.com

      #3
      Re: bit parsing from file

      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.hexlif y(data) � � � � � �# BB9A (10111011100110 10)
      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

      Comment

      • rewonka@gmail.com

        #4
        Re: bit parsing from file

        On May 30, 9:16 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
        On Thu, 29 May 2008 09:37:54 -0700 (PDT), "rewo...@gmail. com"
        <rewo...@gmail. comdeclaimed the following in comp.lang.pytho n:
        >
        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(in byte)
        >
        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.netc om.com              wulfr...@bestia ria.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

        Comment

        Working...