title: Reading and writing binary data of dynamic sizes question
body: <p>I am trying to read data from a file in binary mode and manipulate that data.  </p>
  
  <pre><code> try:
    resultfile = open("binfile", "rb")
   except:
    print "Error"
   resultsize = os.path.getsize("binfile")
  </code></pre>
  
  <p>There is a 32 byte header which I parse fine then the buffer of binary data starts.  The data can be of any size from 16 to 4092 and can be in any format from text to a pdf or image or anything else.  The header has the size of the data so to get this information I do</p>
  
  <pre><code>contents = resultfile.read(resultsize)
  </code></pre>
  
  <p>and this puts the entire file into a string buffer.  I found out this is probably my problem because when I try to copy chunks of the hex data from "contents" into a new file some bytes do not copy correctly so pdfs and images will come out corrupted.  </p>
  
  <p>Printing out a bit of the file string buffer in the interpreter yields for example something like "%PDF-1.5\r\n%\xb5\xb5\xb5\xb5\r\n1 0 obj\r\n" when I just want the bytes themselves in order to write them to a new file.  Is there an easy solution to this problem that I am missing?</p>
  
  <p>Here is an example of a hex dump with the pdf written with my python and the real pdf:</p>
  
  <pre><code>25 50 44 46 2D 31 2E 35 0D 0D 0A 25 B5 B5 B5 B5 0D 0D 0A 31 20 30 20 6F 62 6A 0D 0D 0A
  
  25 50 44 46 2D 31 2E 35 0D    0A 25 B5 B5 B5 B5 0D    0A 31 20 30 20 6F 62 6A
  </code></pre>
  
  <p>It seems like a 0D is being added whenever there is a 0D 0A.  In image files it might be a different byte, I don't remember and might have to test it.
  My code to write the new file is pretty simple, using contents as the string buffer holding all the data.</p>
  
  <pre><code>        fbuf = contents[offset+8:size+offset]
          fl = open(fname, 'a')
          fl.write(fbuf)
  </code></pre>
  
  <p>This is being called in a loop based on a signature that is found in the header.  Offset+8 is beginning of actual pdf data and size is the size of the chunk to copy.</p>