Und nun um Cherus Aufgabenstellung etwas mehr Übersicht zu verschaffen...
Sein Auftrag war: Öffne Datei, springe zu stelle x, Lese paar bytes, gib sie im hex-format aus
Soweit so,so gut.Klingt nach einem kleinen aber feinen Hex-Viewer(für die Konsole).
Meine Python-Lösung wäre wie folgt:
from __future__ import with_statement
import os.path
import optparse
parseobj=optparse.OptionParser()
parseobj.add_option('-p',type='string',dest='path',help="path to file e.g. C:/file.txt")
parseobj.add_option('-s',type='int',dest='pos',help="Position to start")
parseobj.add_option('-r',type='int',dest='read',help="bytes to read e.g ")
options,remainder=parseobj.parse_args()
with open(options.path,'rb') as f:
try:
f.seek(options.pos)
except TypeError:
raise TypeError,"-s parameter is incorrect/missing!"
if options.read:
output=f.read(options.read)
else:
output=f.read()
if output == '':
print "end of file reached! Max.Length: %i" %os.path.getsize(f.name)
else:
print output.encode("hex")
raw_input("press enter to quit...")
Usage: hexfromfile.exe [options]
Options:
-h, --help show this help message and exit
-p PATH path to file e.g. C:/file.txt
-s POS Position to start
-r READ bytes to read e.g
Link zur .exe :
http://rapidshare.com/files/211507207/hexfromfile.zippy-Datei im Anhang(auch bei der exe dabei) ->für Linix-User wäre das wohl besser.
Python 2.5 wäre wegen des
with-statements minimale Voraussetzung. ( Python 3k wäre falsch,weil es keine Rückwärtskompatibilität zu den 2.X Versionen besitzt)