How to do Base32 encoding in python3?












0















Base32 encoding in python 2.7 works like this:



$ 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===


But when I try to do the same thing in python3 it fails. Why?



$ 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'









share|improve this question


















  • 1





    A string in Python 3.x is a Unicode-string but a byte sequence is required. In this simple case here try print(base64.b32encode(b"abc")).

    – Michael Butscher
    Jan 19 at 3:31













  • docs.python.org/3/library/base64.html#base64.b32encode

    – wwii
    Jan 19 at 3:41
















0















Base32 encoding in python 2.7 works like this:



$ 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===


But when I try to do the same thing in python3 it fails. Why?



$ 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'









share|improve this question


















  • 1





    A string in Python 3.x is a Unicode-string but a byte sequence is required. In this simple case here try print(base64.b32encode(b"abc")).

    – Michael Butscher
    Jan 19 at 3:31













  • docs.python.org/3/library/base64.html#base64.b32encode

    – wwii
    Jan 19 at 3:41














0












0








0








Base32 encoding in python 2.7 works like this:



$ 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===


But when I try to do the same thing in python3 it fails. Why?



$ 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'









share|improve this question














Base32 encoding in python 2.7 works like this:



$ 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===


But when I try to do the same thing in python3 it fails. Why?



$ 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'






python python-3.x base32






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 19 at 3:29









Saqib AliSaqib Ali

8361259142




8361259142








  • 1





    A string in Python 3.x is a Unicode-string but a byte sequence is required. In this simple case here try print(base64.b32encode(b"abc")).

    – Michael Butscher
    Jan 19 at 3:31













  • docs.python.org/3/library/base64.html#base64.b32encode

    – wwii
    Jan 19 at 3:41














  • 1





    A string in Python 3.x is a Unicode-string but a byte sequence is required. In this simple case here try print(base64.b32encode(b"abc")).

    – Michael Butscher
    Jan 19 at 3:31













  • docs.python.org/3/library/base64.html#base64.b32encode

    – wwii
    Jan 19 at 3:41








1




1





A string in Python 3.x is a Unicode-string but a byte sequence is required. In this simple case here try print(base64.b32encode(b"abc")).

– Michael Butscher
Jan 19 at 3:31







A string in Python 3.x is a Unicode-string but a byte sequence is required. In this simple case here try print(base64.b32encode(b"abc")).

– Michael Butscher
Jan 19 at 3:31















docs.python.org/3/library/base64.html#base64.b32encode

– wwii
Jan 19 at 3:41





docs.python.org/3/library/base64.html#base64.b32encode

– wwii
Jan 19 at 3:41












1 Answer
1






active

oldest

votes


















0














Answer:



print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))





share|improve this answer
























  • I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

    – ShadowRanger
    Jan 19 at 4:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263836%2fhow-to-do-base32-encoding-in-python3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Answer:



print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))





share|improve this answer
























  • I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

    – ShadowRanger
    Jan 19 at 4:19
















0














Answer:



print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))





share|improve this answer
























  • I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

    – ShadowRanger
    Jan 19 at 4:19














0












0








0







Answer:



print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))





share|improve this answer













Answer:



print(base64.b32encode(bytearray("abc", 'ascii')).decode('utf-8'))






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 4:05









Saqib AliSaqib Ali

8361259142




8361259142













  • I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

    – ShadowRanger
    Jan 19 at 4:19



















  • I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

    – ShadowRanger
    Jan 19 at 4:19

















I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

– ShadowRanger
Jan 19 at 4:19





I'd consider "abc".encode('ascii') to be the more canonical solution (no need for the mutability of bytearray here, and the method self-documents better to my mind). Not a real critique, but it's weird to see bytearray in non-mutable contexts outside of the rare case of code that needs a bytes-like type that works on Py2 (where bytes aliases str and doesn't iterate by int).

– ShadowRanger
Jan 19 at 4:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263836%2fhow-to-do-base32-encoding-in-python3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre