Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File is not decoded properly

Tags:

python

I have a file encoded in a strange pattern. For example,

Char (1 byte) | Integer (4 bytes) | Double (8 bytes) | etc...

So far, I wrote the code below, but I have not been able to figure out why still shows garbage in the screen. Any help will be greatly appreciated.

BRK_File = 'commands.BRK'
input = open(BRK_File, "rb")

rev = input.read(1)
filesize = input.read(4)
highpoint = input.read(8)
which = input.read(1)

print 'Revision: ', rev 
print 'File size: ', filesize
print 'High point: ', highpoint
print 'Which: ', which

while True
    opcode = input.read(1)
    print 'Opcode: ', opcode
    if opcode = 120:
         break
    elif
        #other opcodes
like image 782
Peretz Avatar asked Oct 04 '11 19:10

Peretz


People also ask

How do you decode a PDF file?

To decode the PDF, or make the text editable in programs such as Microsoft Word or Google Documents, an intermediate program such as Acrobat Pro, or the less expensive Foxit Reader, must be used.

How do I uncorrupt a PDF file?

If the PDF still doesn't work after updating Acrobat Reader, go to Help > Repair installation. Restore previous version. Another method to repairing a damaged PDF is restoring it to a previous version. Head to the location where the PDF is saved, right click on the file and select Restore previous versions.

How do you open a PDF that says it's damaged?

Step 1- Press Windows Key + X, and a menu will appear select Control Panel. Step 2- Go to the program and click on Uninstall a program. Step 3- Go for Adobe Acrobat Reader, then right-clicks and select Change from the menu. Step 4- Now click next and then select the Repair option.

Why is my PDF file unsupported?

Unsupported file type: This is the most common reason you cannot open PDF. Occasionally some files may erroneously have the default application set to Adobe Reader. This problem is usually an inadvertent human error. Outdated Acrobat or Adobe Reader: An outdated Adobe Reader or Acrobat program will throw this error.


1 Answers

read() returns a string, which you need to decode to get the binary data. You could use the struct module to do the decoding.

Something along the following lines should do the trick:

import struct
...
fmt = 'cid' # char, int, double
data = input.read(struct.calcsize(fmt))
rev, filesize, highpoint = struct.unpack(fmt, data)

You may have to deal with endianness issues, but struct makes that pretty easy.

like image 52
NPE Avatar answered Sep 17 '22 17:09

NPE