Last active
March 19, 2017 00:32
-
-
Save Fusion86/decb8c38da58d0a1d97fc00d53b50764 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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