【发布时间】:2011-10-07 08:36:10
【问题描述】:
如何在 Python 3.2 中将十六进制字符串转换为有符号整数?
我能想到的最好的是
h = '9DA92DAB'
b = bytes(h, 'utf-8')
ba = binascii.a2b_hex(b)
print(int.from_bytes(ba, byteorder='big', signed=True))
有没有更简单的方法?无符号就简单多了:int(h, 16)
顺便说一句,问题的出处是itunes persistent id - music library xml version and iTunes hex version
【问题讨论】:
-
这两行 b= 和 ba= 可以用 ba=bytes.fromhex(h) 代替。请参阅下面 Lennart 的评论。
标签: integer python-3.x hex