I am creating this "hexdump" program, and the problem that I am having is writing in the file because I need it to dump the data in a file instead of keeping it in the terminal.
P.S: I'm a bit new to Python, so my friend helped me in coding this.
Here is the code:
import sys
import pickle
def hexdump(fname, start, end, width):
for line in get_lines(fname, int(start), int(end), int(width)):
nums = ["%02x" % ord(c) for c in line]
txt = [fixchar(c) for c in line]
x = " ".join(nums), "".join(txt)
y = ' '.join(x)
print (y)
f = open('dump.txt', 'w')
pickle.dump(y, f)
f.close()
def fixchar(char):
from string import printable
if char not in printable[:-5]:
return "."
return char
def get_lines(fname, start, end, width):
f = open(fname, "rb")
f.seek(start)
chunk = f.read(end-start)
gap = width - (len(chunk) % width)
chunk += gap * '\000'
while chunk:
yield chunk[:width]
chunk = chunk[width:]
if __name__ == '__main__':
try:
hexdump(*sys.argv[1:5])
except TypeError:
hexdump("hexdump.py", 0, 100, 16)
I know that it is quite a mess, but I need to print the data in a file.