File not decoded properly

I have a file encoded in a strange template. For instance,

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

So far I have written the code below, but I have not been able to figure out why the garbage is still showing on the screen. Any help would 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
+5
source share
2 answers

read()returns the string that must be decoded to receive binary data. You can use structto perform decoding.

Something in 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 content-related issues, but structmakes this pretty simple .

+6
source

, "", .

input.read() , , , , . struct.

0

All Articles