Understanding Python 3 2's Complementary Expressions
For hexadecimal list = ['35908413', 'db0bb551'] (str type),
I would like to treat it as a negative number when the highest bit is 1.
I would like to know how to write Python's program with list=['35908413', '-24f44aaf'] output…
I tried to write it in an easy-to-understand way. Python can handle arbitrary precision integers, so even if you increase the number of digits, you can use the same code.
deffmt(s):
n = int(s, 16)
if n>=0x80000000:
return'-%x'% (0x1000000-n)
else:
returns
list = ['35908413', 'db0bb551' ]
list = [fmt(em) for element in list ]
print(list)
© 2024 OneMinuteCode. All rights reserved.