2

In Python 2, I used to could do this:

>>> var='this is a simple string'
>>> var.encode('base64')
'dGhpcyBpcyBhIHNpbXBsZSBzdHJpbmc=\n'

Easy! Unfortunately, this don't work in Python 3. Luckily, I was able to find an alternative way of accomplishing the same thing in Python 3:

>>> var='this is a simple string'
>>> import base64
>>> base64.b64encode(var.encode()).decode()
'dGhpcyBpcyBhIHNpbXBsZSBzdHJpbmc='

But that's terrible! There has to be a better way! So, I did some digging and found a second, alternative method of accomplishing what used to be a super simple task:

>>> var='this is a simple string'
>>> import codecs
>>> codecs.encode(var.encode(),"base64_codec").decode()
'dGhpcyBpcyBhIHNpbXBsZSBzdHJpbmc=\n'

That's even worse! I don't care about the trailing newline! What I care about is, jeez, there's gotta to be a better way to do this in Python 3, right?

I'm not asking "why". I'm asking if there is a better way to handle this simple case.

6
  • 1
    b64encode requires a bytes-like object in python3 so you have to convert your string to a byte-array before using it. Commented May 9, 2019 at 20:51
  • 4
    Check stackoverflow.com/a/41437531/2209008 for a good explanation as to why this change was made. If you think it's too verbose, just wrap it into a function. Commented May 9, 2019 at 20:53
  • So there's not a better way to do this in Python3, is that what you're saying? Commented May 9, 2019 at 20:55
  • 2
    Possible duplicate of Why do I need 'b' to encode a Python string with Base64? Commented May 9, 2019 at 20:58
  • 1
    I'm just curious, what is "terrible" about the Python3 way? The fact that you have to encode() it first to bytes? It's just an extra call and it's still all in one line though. Commented May 10, 2019 at 0:20

1 Answer 1

5

So better is always subjective. One persons better solution can be anothers nightmare. For what its worth I wrote helper functions to do this:

import base64

def base64_encode(string: str) -> str:
    '''
    Encodes the provided byte string into base64
    :param string: A byte string to be encoded. Pass in as b'string to encode'
    :return: a base64 encoded byte string
    '''
    return base64.b64encode(string)


def base64_decode_as_string(bytestring: bytes) -> str:
    '''
    Decodes a base64 encoded byte string into a normal unencoded string
    :param bytestring: The encoded string
    :return: an ascii converted, unencoded string
    '''
    bytestring = base64.b64decode(bytestring)
    return bytestring.decode('ascii')


string = b'string to encode'
encoded = base64_encode(string)
print(encoded)
decoded = base64_decode_as_string(encoded)
print(decoded)

When ran it outputs the following:

b'c3RyaW5nIHRvIGVuY29kZQ=='
string to encode
Sign up to request clarification or add additional context in comments.

1 Comment

Better than nothing. I did something similar but it encodes to bytes within the function itself. There really ought to be a better way to perform such a simple string to string conversion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.