incrementing string/hex value from file and write back

incrementing string/hex value from file and write back

Post by Matthias G » Sat, 22 Aug 2009 06:08:34


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

incrementing string/hex value from file and write back

Post by Rami Chowd » Sat, 22 Aug 2009 06:18:28

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





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

 
 
 

incrementing string/hex value from file and write back

Post by Simon Form » Sat, 22 Aug 2009 06:35:12


Check this out:

In [1]: val = '0C'

In [2]: val.encode('hex')
Out[2]: '3043'

That's not what you want. Try this:

In [3]: int(val, 16)
Out[3]: 12

And to convert an int to a hex string.

In [4]: '%x' % 13
Out[4]: 'd'

The interpreter has a help() function that gives you quick access to
information about python objects:
>>>> help(str.encode)
Help on method_descriptor:

encode(...)
S.encode([encoding[,errors]]) >> object

Encodes S using the codec registered for encoding. encoding
defaults
to the default encoding. errors may be given to set a different
error
handling scheme. Default is 'strict' meaning that encoding errors
raise
a UnicodeEncodeError. Other possible values are 'ignore',
'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that is able to handle UnicodeEncodeErrors.
>>>> help(int)
Help on class int in module __builtin__:

class int(object)
| int(x[, base]) >> integer
|
| Convert a string or number to an integer, if possible. A floating
point
| argument will be truncated towards zero (this does not include a
string
| representation of a floating point number!) When converting a
string, use
| the optional base. It is an error to supply a base when
converting a
| non-string. If the argument is outside the integer range a long
object
| will be returned instead.
|
| Methods defined here:
|
....


Unfortunately you can't use it on the '%' string formatting
operator...
>>>> help(%)
SyntaxError: invalid syntax

So here's a link to the docs:
http://www.yqcomputer.com/ #string-formatting-operations


HTH,
~Simon
 
 
 

incrementing string/hex value from file and write back

Post by Mark Lawre » Sat, 22 Aug 2009 06:36:37


val = int(val, 16) + 1
f.write('%02X' % val)


--
Kindest regards.

Mark Lawrence.
 
 
 

incrementing string/hex value from file and write back

Post by Ethan Furm » Sat, 22 Aug 2009 06:36:41

[fixed top-posting]




.encode('hex') is returning a string -- attempting to add one to it will
raise the same error the OP is getting below.

To get a number you can do (after reading val from the file):

val = int(val, '16') # convert from base 16
val += 1 # increment
val = "%X" % val # back to heg digits

and then write it back out again. Don't forget to close and reopen the
file for writing. :)

~Ethan~
 
 
 

incrementing string/hex value from file and write back

Post by Simon Form » Sat, 22 Aug 2009 06:38:08


No, it returns another string, which still isn't the decimal
representation of the hex string. hex C >> decimal 12

In [1]: '0C'.encode('hex')
Out[1]: '3043'
 
 
 

incrementing string/hex value from file and write back

Post by Rami Chowd » Sat, 22 Aug 2009 06:44:57

Of course - my apologies, I was being an idiot.

On Thu, 20 Aug 2009 14:38:08 -0700, Simon Forman < XXXX@XXXXX.COM >






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

incrementing string/hex value from file and write back

Post by Dave Ange » Sat, 22 Aug 2009 08:05:17


You don't say much to constrain the file. Is it always two characters
(nibbles) long? Or might it have a newline at the end? If it could be
multiple lines, is each line limited to 2 columns? or to 8 columns? or
no limit?

To interpret val as an integer, try new = int(val, 16)
Then to convert back to hex digits, try line = "%x" % (val+1)

For fancier conversions, look at binascii.unhexlify() and hexlify().
Look also at chr() and ord(). and str.format()

DaveA