Binary handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derfel

    Binary handling

    Hello all,
    I have a file of binary data. I want to read this file and parse the data in
    it. The format of the file is:
    8 bits number of data pakets
    6 bits offset
    20 bits time
    8 states

    How can I read this fields in binary? I see the struct module and the
    binascii, but I can't split the diferent fiels.

    Thanks in advance
    Regards
    --
    Defel
  • Diez B. Roggisch

    #2
    Re: Binary handling

    Derfel wrote:
    [color=blue]
    > Hello all,
    > I have a file of binary data. I want to read this file and parse the data
    > in it. The format of the file is:
    > 8 bits number of data pakets
    > 6 bits offset
    > 20 bits time
    > 8 states
    >
    > How can I read this fields in binary? I see the struct module and the
    > binascii, but I can't split the diferent fiels.[/color]

    You can use struct to create a long from your data and then work on that
    with the usual bitwise operators for shift and boolean and so that you
    extract the actual data. If the above is the entire spec, I see some
    problems to get the alignment proper, but with a bit of shifting you should
    be able to work around that.


    --
    Regards,

    Diez B. Roggisch

    Comment

    • Miki Tebeka

      #3
      Re: Binary handling

      Hello Derfel,
      [color=blue]
      > I have a file of binary data. I want to read this file and parse the data in
      > it. The format of the file is:
      > 8 bits number of data pakets
      > 6 bits offset
      > 20 bits time
      > 8 states
      >
      > How can I read this fields in binary? I see the struct module and the
      > binascii, but I can't split the diferent fiels.[/color]
      --- bitter.py ---
      # Testing
      '''[color=blue][color=green][color=darkred]
      >>> b = BitStream(chr(i nt("10111010", 2)))
      >>> b.get_bits(3)[/color][/color][/color]
      5[color=blue][color=green][color=darkred]
      >>> b.get_bits(2)[/color][/color][/color]
      3[color=blue][color=green][color=darkred]
      >>> b.get_bits(3)[/color][/color][/color]
      2[color=blue][color=green][color=darkred]
      >>> b.get_bits(0)[/color][/color][/color]
      0[color=blue][color=green][color=darkred]
      >>> b.get_bits(1)[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "/tmp/bitter.py", line 27, in get_bits
      raise EOFError
      EOFError
      '''

      from array import array

      class BitStream:
      '''Bit stream implementation' ''
      def __init__(self, data):
      self.arr = array("c", data)
      self.bit = 0 # Current output bit

      def get_bits(self, count):
      '''Get `count' bits from stream'''
      if count == 0: # Nothing to get
      return 0

      if not self.arr: # EOF
      raise EOFError

      value = 0
      for c in range(count):
      if not self.arr: # EOF
      raise EOFError
      value <<= 1
      shifted = ord(self.arr[0]) >> (8 - self.bit - 1)
      value |= (shifted & 1)
      self.bit += 1
      if self.bit == 8: # Get new byte
      self.arr.pop(0)
      self.bit = 0

      return value

      def _test():
      from doctest import testmod
      import bitter

      testmod(bitter)

      if __name__ == "__main__":
      _test()
      --- bitter.py ---

      HTH.
      Miki

      Comment

      • Derfel

        #4
        Re: Binary handling

        Miki Tebeka escribió en news:40A8D7F3.7 080400@zoran.co m:
        [color=blue]
        > Hello Derfel,
        >
        > --- bitter.py ---[/color]

        Thanks for the module, it works well and is the solution to my problem.
        Thanks.
        Regards
        --
        Derfel

        Comment

        Working...