incrementing string/hex value from file and write back

2009-08-20 Thread Matthias Güntert
Hello guys I would like to read a hex number from an ASCII file, increment it and write it back. How can this be performed? I have tried several approaches: my file serial.txt contains: 0C -- f = open('serial.txt', 'r') val = f.read() val = val.encode('hex')

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Rami Chowdhury
val = val.encode('hex') That's the crucial line -- it's returning a new integer, which you are re-binding to val. If you then did: val = val + 1 you'd be fine, and could then write val back to your file :-) On Thu, 20 Aug 2009 14:08:34 -0700, Matthias Güntert matzeguent...@gmx.de

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Mark Lawrence
Matthias Güntert wrote: Hello guys I would like to read a hex number from an ASCII file, increment it and write it back. How can this be performed? I have tried several approaches: my file serial.txt contains: 0C -- f = open('serial.txt', 'r') val =

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Simon Forman
On Aug 20, 5:08 pm, Matthias Güntert matzeguent...@gmx.de wrote: Hello guys I would like to read a hex number from an ASCII file, increment it and write it back. How can this be performed? I have tried several approaches: my file serial.txt contains: 0C

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Simon Forman
On Aug 20, 5:18 pm, Rami Chowdhury rami.chowdh...@gmail.com wrote: val = val.encode('hex') That's the crucial line -- it's returning a new integer, which you are   re-binding to val. If you then did: No, it returns another string, which still isn't the decimal representation of the hex

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Ethan Furman
[fixed top-posting] Rami Chowdhury wrote: On Thu, 20 Aug 2009 14:08:34 -0700, Matthias Güntert matzeguent...@gmx.de wrote: Hello guys I would like to read a hex number from an ASCII file, increment it and write it back. How can this be performed? I have tried several approaches: my file

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Rami Chowdhury
Of course - my apologies, I was being an idiot. On Thu, 20 Aug 2009 14:38:08 -0700, Simon Forman sajmik...@gmail.com wrote: On Aug 20, 5:18 pm, Rami Chowdhury rami.chowdh...@gmail.com wrote: val = val.encode('hex') That's the crucial line -- it's returning a new integer, which you are  

Re: incrementing string/hex value from file and write back

2009-08-20 Thread Dave Angel
Matthias Güntert wrote: Hello guys I would like to read a hex number from an ASCII file, increment it and write it back. How can this be performed? I have tried several approaches: my file serial.txt contains: 0C -- f = open('serial.txt', 'r') val =