pythons “struct.unpack / pack” in c#

hey
i want to rewrite an python programm into c#. This programm is supposed to edit an gamefile that is packed a specific way. the programming language "python" has the function struct.unpack for accessing this file but a similar function is missing in c#. is there a way to emulate this function via c# code ? here is an example code:

 stb = open(path, 'rb').read() # Room count rooms = struct.unpack_from('<I', stb, 0)[0] off = 4 ret = [] for room in range(rooms): # Room Type, Room Variant, Difficulty, Length of Room Name String roomData = struct.unpack_from('<IIBH', stb, off) off += 11 # Room Name roomName = struct.unpack_from('<{0}s'.format(roomData[3]), stb, off)[0].decode() off += roomData[3] # Weight, width, height, number of doors, number of entities entityTable = struct.unpack_from('<fBBBH', stb, off) off += 9 

example code for repacking things:

 stb = open(path, 'wb') out = struct.pack('<I', len(rooms)) for room in rooms: out += struct.pack('<IIBH{0}sfBB'.format(len(room.text())), room.roomType, room.roomVariant, room.roomDifficulty, len(room.text()), room.text().encode(), room.roomWeight, room.roomWidth, room.roomHeight) 

Thanks for helping me out 🙂

by Wofsauge via /r/csharp

Leave a Reply