python 中如何处理极大的值?
我如何让 python 处理 1099511627776 位大小的值(是的,137 GB)?我有些需要实现这个(或者如果你能建议一个更好的方法来做到这一点,我会改变方法)。显然,pgp 的新长度类型有 3 个部分,而不是 2 个。现在它们是:长度类型、长度类型值和长度。 length 类型为 2 位,可转换为 191 字节、8383 字节、4294967296 字节或部分长度。然后长度以字节为单位进行编码。如果我什至不能执行1 <<,我该如何检查一个值是否小于 4294967296 字节大(4294967296 << 8)
?它太大了,甚至长的都装不下。
How would I get python to work with values of the order of magnitude of 1099511627776 bits large (yeah. 137 gb)? I some what need to implement this (or if you can suggest a better way to do it, will change methods). apparently, pgp's new length types have 3 sections instead of 2. now they are: length type, value-of-length-type, and the length. length type is 2 bits, which translates to 191 bytes, 8383 bytes, 4294967296 bytes, or partial length. the length is then encoded in bytes. how would i check if a value is less than 4294967296 bytes large if i cant even do 1 << (4294967296 << 8)
? it is too big to fit in even a long.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不仅太大,无法容纳很长的内容,而且太大,无法容纳任何计算机的内存。我认为你误解了一些东西。
据我了解,最大的键值是 4,294,967,295 字节。那是 4GB,而不是 137GB。您在内存中保存的密钥不是作为一个数字,而是作为一串字节。所以我不知道你从哪里得到一个 137 GB 大的数字。
如果PGP有这样的要求,那是不可能实现的。既然有实现,我确信这不是它的实现方式。
(另外,我确信 Python 有 PGP 模块,但如果你这样做不是因为你需要它,而是为了练习并且因为你想学习,那么就坚持下去!)
It's not only to big to fit in a long, it's too big to fit in the memory of any computer. I think you misunderstood something.
As I understand it, the largest key value is 4,294,967,295 bytes. That's 4GB, not 137 GB. You hold that key in memory not as one number, but as a string of bytes. So I don't know where you get a number that's 137 GB large from.
If PGP required that, it would be impossible to implement. Since there are implementations, I'm sure that's not how it is done.
(Also, I'm sure there are PGP modules for Python, but if you are doing this not because you need it, but for practice and because you want to learn, then keep it up!)
采用三参数形式
pow()
。With the three-argument form of
pow()
.使用大数字库,例如 GMPY。
Use a big number library like GMPY.
我对 RFC4880(2007 年 11 月)的解释不同。第 3.2 节将多精度整数描述为具有 2 个八位字节长度,因此最大大小为 64KB。第 4.2.2 节描述了新的数据包格式和您所描述的规模的文档编号。但数据包格式与多精度整数格式不同。如果您有不同的解释,请使用您正在阅读的 RFC 的确切部分更新您的问题。
I interpret RFC4880 (November 2007) differently. Section 3.2 describes multiple precision integers as having a 2 octet length so the largest size would be 64KB. Section 4.2.2 describes the new packet format and documents numbers of the scale you are describing. But the packet format is not the same as the multiple precision integer format. If you are interpreting it differently, please update your question with the exact sections of the RFC that you are reading.