将字节转换为整数?
Convert bytes to int?
我目前正在开发一个 encryption/decryption 程序,我需要能够将字节转换为整数。我知道:
bytes([3]) = b'\x03'
但我不知道如何进行逆运算。我做错了什么?
假设您至少使用 3.2,则有 built in for this:
int.from_bytes
( bytes
, byteorder
, *, signed=False
)
...
The argument bytes
must either be a bytes-like object or an iterable
producing bytes.
The byteorder
argument determines the byte order used to represent the
integer. If byteorder
is "big"
, the most significant byte is at the
beginning of the byte array. If byteorder
is "little"
, the most
significant byte is at the end of the byte array. To request the
native byte order of the host system, use sys.byteorder
as the byte
order value.
The signed
argument indicates whether two’s complement is used to
represent the integer.
## Examples:
int.from_bytes(b'\x00\x01', "big") # 1
int.from_bytes(b'\x00\x01', "little") # 256
int.from_bytes(b'\x00\x10', byteorder='little') # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) #-1024
int.from_bytes( bytes, byteorder, *, signed=False )
不适合我
我使用了这个网站的功能,效果很好
https://coderwall.com/p/x6xtxq/convert-bytes-to-int-or-int-to-bytes-in-python
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
def int_to_bytes(value, length):
result = []
for i in range(0, length):
result.append(value >> (i * 8) & 0xff)
result.reverse()
return result
字节列表是可订阅的(至少在 Python 3.6 中是这样)。这样您就可以单独检索每个字节的十进制值。
>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes(intlist) # b'@\x04\x1a\xa3\xff'
>>> for b in bytelist:
... print(b) # 64 4 26 163 255
>>> [b for b in bytelist] # [64, 4, 26, 163, 255]
>>> bytelist[2] # 26
在处理缓冲数据的情况下,我发现这很有用:
int.from_bytes([buf[0],buf[1],buf[2],buf[3]], "big")
假设buf
中的所有元素都是8位长。
list()
可用于将字节转换为 int(适用于 Python 3.7):
list(b'\x03\x04\x05')
[3, 4, 5]
我在寻找现有解决方案时偶然发现的一个老问题。滚动我自己并认为我会分享,因为它允许您从字节列表创建一个 32 位整数,指定一个偏移量。
def bytes_to_int(bList, offset):
r = 0
for i in range(4):
d = 32 - ((i + 1) * 8)
r += bList[offset + i] << d
return r
我目前正在开发一个 encryption/decryption 程序,我需要能够将字节转换为整数。我知道:
bytes([3]) = b'\x03'
但我不知道如何进行逆运算。我做错了什么?
假设您至少使用 3.2,则有 built in for this:
int.from_bytes
(bytes
,byteorder
, *,signed=False
)...
The argument
bytes
must either be a bytes-like object or an iterable producing bytes.The
byteorder
argument determines the byte order used to represent the integer. Ifbyteorder
is"big"
, the most significant byte is at the beginning of the byte array. Ifbyteorder
is"little"
, the most significant byte is at the end of the byte array. To request the native byte order of the host system, usesys.byteorder
as the byte order value.The
signed
argument indicates whether two’s complement is used to represent the integer.
## Examples:
int.from_bytes(b'\x00\x01', "big") # 1
int.from_bytes(b'\x00\x01', "little") # 256
int.from_bytes(b'\x00\x10', byteorder='little') # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) #-1024
int.from_bytes( bytes, byteorder, *, signed=False )
不适合我 我使用了这个网站的功能,效果很好
https://coderwall.com/p/x6xtxq/convert-bytes-to-int-or-int-to-bytes-in-python
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
def int_to_bytes(value, length):
result = []
for i in range(0, length):
result.append(value >> (i * 8) & 0xff)
result.reverse()
return result
字节列表是可订阅的(至少在 Python 3.6 中是这样)。这样您就可以单独检索每个字节的十进制值。
>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes(intlist) # b'@\x04\x1a\xa3\xff'
>>> for b in bytelist:
... print(b) # 64 4 26 163 255
>>> [b for b in bytelist] # [64, 4, 26, 163, 255]
>>> bytelist[2] # 26
在处理缓冲数据的情况下,我发现这很有用:
int.from_bytes([buf[0],buf[1],buf[2],buf[3]], "big")
假设buf
中的所有元素都是8位长。
list()
可用于将字节转换为 int(适用于 Python 3.7):
list(b'\x03\x04\x05')
[3, 4, 5]
我在寻找现有解决方案时偶然发现的一个老问题。滚动我自己并认为我会分享,因为它允许您从字节列表创建一个 32 位整数,指定一个偏移量。
def bytes_to_int(bList, offset):
r = 0
for i in range(4):
d = 32 - ((i + 1) * 8)
r += bList[offset + i] << d
return r