1

python 2.7 中的 Base32 编码是这样工作的:

$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> print(base64.b32encode("abc"))
MFRGG===

但是当我尝试在 python3 中做同样的事情时它失败了。为什么?

$ python
Python 3.7.2 (default, Jan 13 2019, 12:50:15)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> print(base64.b32encode("abc"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "my-virtual-env/lib/python3.7/base64.py", line 154, in b32encode
    s = memoryview(s).tobytes()
TypeError: memoryview: a bytes-like object is required, not 'str'
4

1 回答 1

1

回答:

print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))
于 2019-01-19T04:05:05.063 回答