How to convert Binary into Text String with Base 64 Encoding in Python

Starting from scratch on base 64 encoding is well and truly its own little can of worms. So why even open that can, when somebody else can do it for you? With a little help from an API, we can skip the entire mess, yet end up with the exact same functionality. Let me show you how it’s done.

With pip install, set up our client for documents and file conversion:

pip install cloudmersive-convert-api-client

Now here’s how to set up our function call for edit_text_base64_encode from our API.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditTextApi(cloudmersive_convert_api_client.ApiClient(configuration))request = cloudmersive_convert_api_client.Base64EncodeRequest() # Base64EncodeRequest | Input requesttry:# Base 64 encode, convert binary or file data to a text stringapi_response = api_instance.edit_text_base64_encode(request)pprint(api_response)except ApiException as e:print("Exception when calling EditTextApi->edit_text_base64_encode: %s\n" % e)

And now our binary input can be fed into edit_text_base64_encode to be converted and returned by the API. Easy mode!

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store