How to convert Binary into Text String with Base 64 Encoding in Python
2 min readApr 27, 2020
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 cloudmersive_convert_api_client.rest 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!