【发布时间】:2013-09-08 03:02:39
【问题描述】:
我正在尝试将 int 编码为 base64,我正在这样做:
foo = 1
base64.b64encode(bytes(foo))
预期输出: 'MQ=='
给定输出: b'AA=='
我做错了什么?
编辑:在 Python 2.7.2 中可以正常工作
【问题讨论】:
-
嗯...您使用的是什么版本的 Python?当我执行 base64.b64encode(bytes(1)) 或 foo=1;base64.b64encode(bytes(foo)) 时,我得到'MQ=='。另外,你在哪里运行这个?
-
当我运行你的代码时,我得到了预期的输出。您是否在其他地方重新定义了 foo ?试试 base64.b64encode(b'1')
-
我正在使用 Python 3.3.2
-
已确认:在 Python 3.1.2 中,它会打印出
b'AA=='。问题不是b64encode,而是bytes()。在 Python3 中,bytes(1)返回b'00'。 -
注意:这绝对不是重复的。下面的答案都不是正确的。见 cmets。
标签: python string int base64 encode