Encoding an entire thing into a text string

In some code I want to get into bubble I can encode any format object (because everything is just a series of bytes), with python code that looks like this:

b64 = codecs.encode(thing, ‘base64’)

then I can convert this encoded binary into a text string as follows:
Sb64=b64.decode(‘utf-8’)

I now have a text string that can be transmitted anywhere but is still just my data object, which could have been anything from text, to a jpeg, to a pdf or a movie.

To get it back I just have to do the reverse process
B64=Sb64.encode(‘utf-8’)
thing=codecs.decode(B64, ‘base64’)

I dont need to do this with text field in a thing, but I would need to do this with a field that may be an image. Further, is there a way to do this to an entire thing at once, thus getting a single string of bytes that represents the entire thing?