incrementing string/hex value from file and write back

Rami Chowdhury rami.chowdhury at gmail.com
Thu Aug 20 17:18:28 EDT 2009


> 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  
<MatzeGuentert at 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
>
> ----------------------------------
> f = open('serial.txt', 'r')
> val = f.read()
> val = val.encode('hex')
> print val
> ----------------------------------
> --> 3043
>
> ----------------------------------
> f = open('serial.txt', 'r')
> val = f.read()
> print val
> val = val+1
> ----------------------------------
> --> TypeError: cannot concatenate 'str' and 'int' objects
>
> ----------------------------------
> f = open('serial.txt', 'rb')
> val = f.read()
> val = val + 1
> ----------------------------------
> --> TypeError: cannot concatenate 'str' and 'int' objects
>
>
> hm....
>



-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list