Sign in to follow this  

File Buffer to Struct

This topic is 3096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to convert the C/C++ code found here: http://johanneskopf.de/publications/solid/textures/file_format.txt 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 = file.read(4096)
Here's a test file that can be used to test the loader: http://johanneskopf.de/publications/solid/textures/woodwall.vol. 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 = file.read(4096)

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

class VolumeHeader:
pass

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

This topic is 3096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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