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.

Jon Red
  • 136
  • 1
  • 9
  • 1
    `b64encode` requires a bytes-like object in python3 so you have to convert your string to a byte-array before using it. – rdas May 09 '19 at 20:51
  • 4
    Check https://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. – Colin Basnett May 09 '19 at 20:53
  • So there's *not* a better way to do this in Python3, is that what you're saying? – Jon Red May 09 '19 at 20:55
  • 2
    Possible duplicate of [Why do I need 'b' to encode a Python string with Base64?](https://stackoverflow.com/questions/8908287/why-do-i-need-b-to-encode-a-python-string-with-base64) – ruohola May 09 '19 at 20:58
  • Not asking "why", I'm asking if there's a better way. – Jon Red May 09 '19 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. – Gino Mempin May 10 '19 at 00:20

1 Answers1

4

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
Robert H
  • 11,520
  • 18
  • 68
  • 110
  • 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. – Jon Red Jul 12 '19 at 21:05