Domanda

I've recently gotten back into programming and decided as a project to get me going and motivated I was going to write a character editor for fallout 2. The issue I'm having is after the first few strings I can't seem to pull the data I need using the file offsets or structs.

This is what I am doing. The file I Am working with is www.retro-gaming-world.com/SAVE.DAT

import struct
savefile = open('SAVE.DAT', 'rb')
try:
        test = savefile.read()
finally:
        savefile.close()

print 'Header: ' +  test[0x00:0x18] # returns the save files header description "'FALLOUT SAVE FILE '"
print "Character Name: " + test[0x1D:0x20+4] Returns the characters name "f1nk"
print "Save game name: " + test[0x3D:0x1E+4] # isn't returning the save name "church" like expected
print "Experience: " + str(struct.unpack('>h', test[0x08:0x04])[0]) # is expected to return the current experience but gives the follosing error

output :

Header: FALLOUT SAVE FILE
Character Name: f1nk
Save game name: 
    Traceback (most recent call last):
        File "test", line 11, in <module>
        print "Experience: " + str(struct.unpack('>h', test[0x08:0x04])[0])
    struct.error: unpack requires a string argument of length 2

I've confirmed the offsets but it just isn't returning anything as it is expected.

È stato utile?

Soluzione

test[0x08:0x04] is an empty string because the end index is smaller than the starting index.

For example, test[0x08:0x0A] would give you two bytes as required by the h code.

The syntax for string slicing is s[start:end] or s[start:end:step]. Link to docs

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top