incrementing string/hex value from file and write back

Matthias Güntert MatzeGuentert at gmx.de
Thu Aug 20 23:08:34 CEST 2009


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....




More information about the Python-list mailing list