Hello Python world, I am on Linux and Python 2.7 , I need some help to figure out what is going wrong in the below piece of code
I am trying to replace seven bytes in the hexdumfile at a given offset Program 1 works fine #!/usr/bin/python from qa.utils.easy_popen import run import pdb def replace_hex(fname, offset, newdata): #pdb.set_trace() with open(fname, 'r+b') as f: #Show current contents f.seek(offset) stringdata = f.read(len(newdata)) print 'Current data:' print '%08X: %s\n' % (offset, stringdata.encode('hex')) #Replace data at offset with newdata f.seek(offset) f.write(newdata) f.close() #print the data cmd = 'hexdump -C %s' % (fname) print cmd out, err, ret = run(cmd) print "Hexdump file data for corrupting" print out, err, ret return fname fname = '/tmp/2017-02-20_08-42-22/file1.raw' offset = 0x1 newdata = '64000101057804'.decode('hex') replace_hex(fname, offset, newdata) -200-1# python file_2.py Current data: 00000001: 6400010105789a hexdump -C /tmp/2017-02-20_08-42-22/file1.raw Hexdump file data for corrupting 00000000 06 64 00 01 01 05 78 04 73 1c ab 58 08 f9 8b 3b |.d....x.s..X...;| ==> Replaced 00000010 1e 00 a1 00 01 00 00 00 64 00 01 01 05 78 02 00 |........d....x..| Program 2 ( Doesn't work , some junk character got replaced ) g-200-1# vi file_2.py #!/usr/bin/python from qa.utils.easy_popen import run import pdb def replace_hex(fname, offset, newdata): #pdb.set_trace() with open(fname, 'r+b') as f: #Show current contents f.seek(offset) stringdata = f.read(len(newdata)) print 'Current data:' print '%08X: %s\n' % (offset, stringdata.encode('hex')) #Replace data at offset with newdata f.seek(offset) f.write(newdata) f.close() #print the data cmd = 'hexdump -C %s' % (fname) print cmd out, err, ret = run(cmd) print "Hexdump file data for corrupting" print out, err, ret return fname fname = '/tmp/2017-02-20_08-42-22/file1.raw' offset = 0x10 newdata = "" newdata = '64000101057804' newdata.decode('hex') replace_hex(fname, offset, newdata) g-200-1# python file_2.py Current data: 00000010: 1e00a10001000000640001010578 hexdump -C /tmp/2017-02-20_08-42-22/MOB_file1.raw Hexdump file data for corrupting 00000000 06 64 00 01 01 05 78 04 73 1c ab 58 08 f9 8b 3b |.d....x.s..X...;| 00000010 36 34 30 30 30 31 30 31 30 35 37 38 30 34 02 00 |64000101057804..|===> Some junk values got writen ??? 00000020 e0 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 |................| 00000030 00 03 10 21 bf cd 05 80 00 00 00 00 00 00 00 00 |...!............| 00000040 58 02 00 00 00 00 00 00 00 20 00 00 00 00 00 00 |X........ ......| 00000050 01 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 |................| 00000060 00 00 00 00 00 00 00 00 74 1c ab 58 a0 84 2b 00 |........t..X..+.| 00000070 74 1c ab 58 a0 84 2b 00 fd ff fd ff 00 00 00 00 |t..X..+.........| 00000080 02 04 25 40 04 00 01 64 00 01 01 05 78 04 31 30 |..%@...d....x.10| 00000090 35 37 38 00 02 84 02 64 00 01 01 05 78 30 30 30 |578....d....x000| 000000a0 31 30 30 30 32 00 0f fe 94 ba 15 00 00 00 00 00 |10002...........| 000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 000001d0 00 00 00 00 00 22 ff ff ff ff 20 03 00 00 00 00 |.....".... .....| 000001e0 00 00 00 0b 04 00 0b 01 00 02 01 0a 00 01 00 64 |...............d| 000001f0 06 00 00 01 00 02 97 0a 33 9b 3f 32 f3 62 b1 15 |........3.?2.b..| Questions: 1. The only difference between both the programs the difference are just the below lines. newdata = '64000101057804'.decode('hex') and newdata = "" newdata = '64000101057804' newdata.decode('hex') What is happening here and how do I fix this in program 2 ? for my eyes there doesn't look any difference . question 2: I am using the variable newdata because I can't hardcore the value , I have to keep changing this every time the function is called, will return it as a string help me slove this problem def get_data() : return str(data) new_data =get_data(input) Regards, Ganesh -- https://mail.python.org/mailman/listinfo/python-list