有没有一种更快的方法可以将任意大整数转换为大字节序字节序列? [英] Is there a faster way to convert an arbitrary large integer to a big endian sequence of bytes?
问题描述
我有以下Python代码可以做到这一点:
I have this Python code to do this:
from struct import pack as _pack
def packl(lnum, pad = 1):
if lnum < 0:
raise RangeError("Cannot use packl to convert a negative integer "
"to a string.")
count = 0
l = []
while lnum > 0:
l.append(lnum & 0xffffffffffffffffL)
count += 1
lnum >>= 64
if count <= 0:
return '\0' * pad
elif pad >= 8:
lens = 8 * count % pad
pad = ((lens != 0) and (pad - lens)) or 0
l.append('>' + 'x' * pad + 'Q' * count)
l.reverse()
return _pack(*l)
else:
l.append('>' + 'Q' * count)
l.reverse()
s = _pack(*l).lstrip('\0')
lens = len(s)
if (lens % pad) != 0:
return '\0' * (pad - lens % pad) + s
else:
return s
这大约需要174个usec才能将2**9700 - 1
转换为我的计算机上的字节字符串.如果我愿意使用Python 2.7和Python 3.x特定的bit_length
方法,可以通过在开始时将l
数组预先分配为正确的大小并使用l[something] =
语法而不是l.append
.
This takes approximately 174 usec to convert 2**9700 - 1
to a string of bytes on my machine. If I'm willing to use the Python 2.7 and Python 3.x specific bit_length
method, I can shorten that to 159 usecs by pre-allocating the l
array to be the exact right size at the very beginning and using l[something] =
syntax instead of l.append
.
我能做些什么来加快速度吗?这将用于转换密码术中使用的大质数以及一些(但不是很多)较小的数.
Is there anything I can do that will make this faster? This will be used to convert large prime numbers used in cryptography as well as some (but not many) smaller numbers.
修改
这是目前Python中最快的选项< 3.2,任一方向花费大约一半的时间作为公认的答案:
This is currently the fastest option in Python < 3.2, it takes about half the time either direction as the accepted answer:
def packl(lnum, padmultiple=1):
"""Packs the lnum (which must be convertable to a long) into a
byte string 0 padded to a multiple of padmultiple bytes in size. 0
means no padding whatsoever, so that packing 0 result in an empty
string. The resulting byte string is the big-endian two's
complement representation of the passed in long."""
if lnum == 0:
return b'\0' * padmultiple
elif lnum < 0:
raise ValueError("Can only convert non-negative numbers.")
s = hex(lnum)[2:]
s = s.rstrip('L')
if len(s) & 1:
s = '0' + s
s = binascii.unhexlify(s)
if (padmultiple != 1) and (padmultiple != 0):
filled_so_far = len(s) % padmultiple
if filled_so_far != 0:
s = b'\0' * (padmultiple - filled_so_far) + s
return s
def unpackl(bytestr):
"""Treats a byte string as a sequence of base 256 digits
representing an unsigned integer in big-endian format and converts
that representation into a Python integer."""
return int(binascii.hexlify(bytestr), 16) if len(bytestr) > 0 else 0
在Python 3.2中,int
类具有to_bytes
和from_bytes
函数,它们可以比上述方法更快地完成 .
In Python 3.2 the int
class has to_bytes
and from_bytes
functions that can accomplish this much more quickly that the method given above.
推荐答案
出于完整性以及以后该问题的读者的考虑:
For completeness and for future readers of this question:
从Python 3.2开始,有函数 int.from_bytes()
和 int.to_bytes()
进行bytes
和int
对象以字节顺序选择.
Starting in Python 3.2, there are functions int.from_bytes()
and int.to_bytes()
that perform the conversion between bytes
and int
objects in a choice of byte orders.
这篇关于有没有一种更快的方法可以将任意大整数转换为大字节序字节序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!