File Buffer to Struct

Started by
0 comments, last by Darklighter 14 years, 10 months ago
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

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement
You can use the struct module to read binary data into Python objects. For example:

import structfile = open("woodwall.vol", "rb")data = file.read(4096)FORMAT_STRING = "4si256s?3i"fields = struct.unpack(FORMAT_STRING, data[:struct.calcsize(FORMAT_STRING)])class VolumeHeader:	passheader = VolumeHeader()header.magic, header.version, header.texName, header.wrap, header.volSize, header.numChannels, header.bytesPerChannel = fields# Convert 'bytes' objects into stringsheader.magic = header.magic.decode("ascii")header.texName = header.texName[:header.texName.find(b"\0")].decode("ascii")

This topic is closed to new replies.

Advertisement