Python 将字节转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34009653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert bytes to int?
提问by Vladimir Shevyakov
I'm currently working on an encryption/decryption program and I need to be able to convert bytes to an integer. I know that:
我目前正在开发一个加密/解密程序,我需要能够将字节转换为整数。我知道:
bytes([3]) = b'\x03'
Yet I cannot find out how to do the inverse. What am I doing terribly wrong?
但是我不知道如何做相反的事情。我做错了什么?
采纳答案by Peter DeGlopper
Assuming you're on at least 3.2, there's a built in for this:
假设你至少在 3.2 上,有一个内置的:
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.
int.from_bytes( bytes, byteorder, *, signed=False)
...
参数 bytes 必须是一个类似字节的对象或一个可迭代的生成字节。
byteorder 参数确定用于表示整数的字节顺序。如果 byteorder 为“big”,则最高有效字节位于字节数组的开头。如果 byteorder 为“小”,则最高有效字节位于字节数组的末尾。要请求主机系统的本机字节顺序,请使用 sys.byteorder 作为字节顺序值。
有符号参数指示是否使用二进制补码来表示整数。
## 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
回答by noura selem
int.from_bytes( bytes, byteorder, *, signed=False )
doesn't work with me I used function from this website, it works well
对我不起作用我使用了这个网站的功能,它运行良好
https://coderwall.com/p/x6xtxq/convert-bytes-to-int-or-int-to-bytes-in-python
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
回答by Ronald
Lists of bytes are subscriptable (at least in Python 3.6). This way you can retrieve the decimal value of each byte individually.
字节列表是可下标的(至少在 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
回答by Ronald
Lists of bytes are subscriptable (at least in Python 3.6). This way you can retrieve the decimal value of each byte individually.
字节列表是可下标的(至少在 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