是否有任何 python 模块可以帮助我将字符串转换为 64 位整数? (此字符串的最大长度为 8 个字符,因此它应该适合一个 long)。
我想避免必须编写自己的方法。
例子:
Input String Hex result (Base-10 Integer)
'Y' 59 89
'YZ' 59 5a 22874
...
最佳答案
这是 struct
的工作:
>>> s = 'YZ'
>>> struct.unpack('>Q', '\x00' * (8 - len(s)) + s)
(22874,)
或者更棘手一点:
>>> int(s.encode('hex'), 16)
22874
关于python - 如何将字符串转换为其 Base-10 表示形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10716796/