I want to add all hexadecimal values in the list and to get hexa decimal value (as 0x44).
I tried to convert the list values to int then added them and then converted back to hex but value was 0x32.
please suggest me way to add the string hexadecimal list to get the hex value.
lst = ['0x0', '0x0', '0x1', '0x1', '0x1', '0x1', '0x1', '0xf', '0xf', '0xf']
total = sum(lst)
Error:
Traceback (most recent call last):
File "./prog.py", line 3, in <module>
TypeError: unsupported operand type(s) for : 'int' and 'str'
CodePudding user response:
You should convert the hex strings into hex values with int() first.
>>> lst = ['0x0', '0x0', '0x1', '0x1', '0x1', '0x1', '0x1', '0xf', '0xf', '0xf']
>>> sum([int(_, 16) for _ in lst])
50
>>> hex(sum([int(_, 16) for _ in lst]))
'0x32'