Sign in to follow this  

File Buffer to Struct

Recommended Posts

Hello, I'm trying to convert the C/C++ code found here: into Python. It looks like the file is loaded, and the header file is read off into a C struct. The contens of the file are then read. I have no idea how to approach this in Python, apart from simply opening the file and reading the first 4096 bytes:
file = open("blah","r")
data =
Here's a test file that can be used to test the loader: Help, anyone? Thanks, Geometrian

Share this post

Link to post
Share on other sites
You can use the struct module to read binary data into Python objects. For example:

import struct

file = open("woodwall.vol", "rb")
data =

FORMAT_STRING = "4si256s?3i"
fields = struct.unpack(FORMAT_STRING, data[:struct.calcsize(FORMAT_STRING)])

class VolumeHeader:

header = VolumeHeader()
header.magic, header.version, header.texName, header.wrap, header.volSize, header.numChannels, header.bytesPerChannel = fields

# Convert 'bytes' objects into strings
header.magic = header.magic.decode("ascii")
header.texName = header.texName[:header.texName.find(b"\0")].decode("ascii")

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this