""" Brief: This file depicts an issue with large-sized ctypes Structure fields. If the size is larger than 65535 bytes it defaults to a bits representation via __repr__()/__str__(). Author: Charles Machalow """ from ctypes import * import inspect class bugStruct(Structure): _pack_ = 1 _fields_ = [ ("correct", c_uint8 * 65535), ("wrongSizeAndOffset", c_uint8 * 999999), ] print (''.join(inspect.getsourcelines(bugStruct)[0])) print("print(str(bugStruct.correct))") print(str(bugStruct.correct) + " # Correct\n") print("print(str(bugStruct.wrongSizeAndOffset))") print(str(bugStruct.wrongSizeAndOffset) + " # Incorrect") print("# Should be: \n") print("To get the math for this do the following on an incorrect size/offset:") print(" size = (bits << 16) + ofs.split(\":\")[1]") print(" ofs = ofs.split(\":\")[0]") print(" Though this isn't really safe because the field may actually be bit-sized")