Python:将数组转换为整数

INI*_*NIX 2 python arrays int

我想知道在Python中是否有办法将数组或列表转换为单个数字:

A = [和0xAA,为0xBB,的0xCC]

d = 0xaabbcc

谢谢你的帮助!

Joh*_*ooy 5

对于Python3 +,int有一个方法

>>> A=[0xaa, 0xbb, 0xcc]
>>> hex(int.from_bytes(A, byteorder="big"))
'0xaabbcc'

>>> 0xaabbcc == int.from_bytes(A, byteorder="big")
True
Run Code Online (Sandbox Code Playgroud)

对于Python2,最好编写一个小函数

>>> A = [0xaa, 0xbb, 0xcc]
>>> 
>>> def to_hex(arr):
...     res = 0
...     for i in arr:
...         res <<= 8
...         res += i
...     return res
... 
>>> 0xaabbcc == to_hex(A)
True
Run Code Online (Sandbox Code Playgroud)