Skip to content

Instantly share code, notes, and snippets.

@Fusion86
Last active March 19, 2017 00:32
Show Gist options
  • Save Fusion86/decb8c38da58d0a1d97fc00d53b50764 to your computer and use it in GitHub Desktop.
Save Fusion86/decb8c38da58d0a1d97fc00d53b50764 to your computer and use it in GitHub Desktop.
import struct
# See https://docs.python.org/2/library/struct.html
typeNames = {
"char": "b",
"uchar": "B",
"short": "h",
"ushort": "H",
"int": "i",
"uint": "I",
"long": "q", # maybe l
"ulong": "Q", # maybe L
"float": "f",
"double": "d",
}
class BinaryReader:
def __init__(self, fileName):
self.file = open(fileName, 'rb')
def read(self, type):
if type != "string":
char = typeNames[type]
size = struct.calcsize(char)
return struct.unpack(char, self.file.read(size))[0]
# We got this far, which means that we are dealing with a string
len = self.read("char")
if len > 128:
len2 = self.read("char")
len = (len % 128) + (len2 * 128)
return self.file.read(len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment