binary conversion issues

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

    #1

    binary conversion issues

    I'm using python's struct and binascii modules to write some values
    from my parser to binary floats. This works great for all of my binary
    files except one. For some reason this file is saving to 836 (stated
    by my command shell) bytes instead of 832 like it should. It sounds
    like an issue with whatever's writing that particular file out but I've
    checked it 100 times. Also python can read in the 836 byte file, find
    it has a size of 832 bytes and convert back each value perfectly.
    However, when I read in the file in C++ it finds a file of size 836 and
    in the middle of the data starts spitting out junk. Has anyone run
    into an issue like this or have an idea of what it could be?

  • Simon Forman

    #2
    Re: binary conversion issues

    godavemon wrote:
    I'm using python's struct and binascii modules to write some values
    from my parser to binary floats. This works great for all of my binary
    files except one. For some reason this file is saving to 836 (stated
    by my command shell) bytes instead of 832 like it should. It sounds
    like an issue with whatever's writing that particular file out but I've
    checked it 100 times. Also python can read in the 836 byte file, find
    it has a size of 832 bytes and convert back each value perfectly.
    However, when I read in the file in C++ it finds a file of size 836 and
    in the middle of the data starts spitting out junk. Has anyone run
    into an issue like this or have an idea of what it could be?
    Not enough information.

    Code and data samples would help.

    Comment

    • Grant Edwards

      #3
      Re: binary conversion issues

      On 2006-08-08, godavemon <davefowler@gma il.comwrote:
      I'm using python's struct and binascii modules to write some values
      from my parser to binary floats. This works great for all of my binary
      files except one. For some reason this file is saving to 836 (stated
      by my command shell) bytes instead of 832 like it should.
      I'm just making a wild-ass guess since you didn't provide any
      sample code or data, but it sounds like cr/lf translation
      problem to me. Make sure you open the files in binary mode
      using the "b" flag.

      --
      Grant Edwards grante Yow! Is this "BIKINI
      at BEACH"?
      visi.com

      Comment

      • godavemon

        #4
        Re: binary conversion issues

        You guys are awesome! I had to set the 'b' flag when writing the
        binaries.

        file_obj = open('filename. bin', 'wb')

        instead of just using 'w'

        It worked fine for all of the other 10 binary files I made, but had a
        small error with one of them. In that one file's case it wrote out an
        extra 4 bytes in the middle somewhere. Strange.

        Thanx for your help.


        Grant Edwards wrote:
        On 2006-08-08, godavemon <davefowler@gma il.comwrote:
        >
        I'm using python's struct and binascii modules to write some values
        from my parser to binary floats. This works great for all of my binary
        files except one. For some reason this file is saving to 836 (stated
        by my command shell) bytes instead of 832 like it should.
        >
        I'm just making a wild-ass guess since you didn't provide any
        sample code or data, but it sounds like cr/lf translation
        problem to me. Make sure you open the files in binary mode
        using the "b" flag.
        >
        --
        Grant Edwards grante Yow! Is this "BIKINI
        at BEACH"?
        visi.com

        Comment

        • Grant Edwards

          #5
          Re: binary conversion issues

          On 2006-08-08, godavemon <davefowler@gma il.comwrote:
          You guys are awesome! I had to set the 'b' flag when writing the
          binaries.
          >
          file_obj = open('filename. bin', 'wb')
          >
          instead of just using 'w'
          >
          It worked fine for all of the other 10 binary files I made,
          but had a small error with one of them. In that one file's
          case it wrote out an extra 4 bytes in the middle somewhere.
          Strange.
          If you leave off the 'b' (and you're running on windows), any
          byte written to the output file that has a value of 0x0A will
          get converted to the two byte sequence 0x0A 0x0D. The extra
          bytes in the resulting file are the 0x0D bytes that were
          inserted after any 0x0A bytes in the written data.

          If the data you write doesn't have any 0x0A bytes, then nothing
          gets changed, and your file contains what you expect. If the
          data does does have 0x0A bytes, you get an extra 0x0D inserted
          after each one.

          (It wasn't _that_ wild-ass a guess, since a lot of people get
          tripped up by the line-ending conversion issue.)

          --
          Grant Edwards grante Yow! Is it clean in other
          at dimensions?
          visi.com

          Comment

          Working...