【问题标题】:Converting Base10 integer to Base64 representation in Python在 Python 中将 Base10 整数转换为 Base64 表示
【发布时间】:2019-12-18 12:58:27
【问题描述】:

这是我的代码,它在第 2 行抛出 MemoryError,没有其他细节。

import base64
b = bytes(2108292477256562115)
print(base64.urlsafe_b64encode(b))

【问题讨论】:

  • b = b'2108292477256562115'b = str(n).encode().

标签: python binary base64 byte


【解决方案1】:

bytes(2108292477256562115)是一个长度为2108292477256562115的字节串。如果你想将该整数转换为最小字节数,你需要先获取表示它所需的字节数:

n = 2108292477256562115
byte_length = (n.bit_length() + 7) // 8

然后转换:

n.to_bytes(byte_length, 'big')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多