0

I have a bunch of hex as strings.

str = "e79"     # 0xe79
str2 = "1533a"  # 0x1533a
etc...

What's the best way to do some addition on these? For example, if I have str = "e79" and I want to add the decimal 37 to it to get str = "e9e"

user2378481
  • 789
  • 1
  • 9
  • 17

2 Answers2

6

You'll have to parse the values into integers if you want to do integer arithmetic; use int(hexstring, 16).

You can always convert back to the hex string representation using format():

result = format(int(strvalue, 16) + 37, 'x')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1
    result = hex(int(str, 16) + int(str2, 16))
sachin saxena
  • 926
  • 5
  • 18