The ESP-IDF contains functions for enabling Base64 encoding and decoding on ESP32. The encoding and decoding functions are actually built into the ESP32 ROM. The functions are accessible by using header files for mbedTLS base64 encoding and decoding.
Base64 related header file
To use base64 encoding and decoding on ESP32, you can include this header file.
#include "mbedtls/base64.h"
Base64 encoding
Here is a snippet that lets you convert a string to base64.
unsigned char *input = "ABCD123";
unsigned char output[64];
size_t outlen;
mbedtls_base64_encode(output, 64, &outlen, input, strlen(input));
The array output contains the base64 encoded text.
The input string is converted and the length of output string length is stored in outlen.
You have to make sure that the output buffer is large enough.
Base64 decoding
Here is a snippet for decoding base64 strings to plaintext.
unsigned char *input = "QUEtQkItQ0MtREQtRUUtRkY6MDAwMA==";
unsigned char output[64];
size_t outlen;
mbedtls_base64_decode(output, 64, &outlen, input, strlen(input));
The array output contains the plain text output.
The input base64 encoded string is converted and the length of output string length is stored in outlen.
Again, ensure that your output buffer is large enough.
2 comments
How would you write audio response from https://texttospeech.googleapis.com/v1/text:synthesize to SPIFF? Do you have an example?
Hi Brenden,
You can refer to audio storage examples like this one:
https://github.com/pcbartists/audiosom32-examples/tree/main/audio-recording
This one writes to the SD card, but the SPIFFS version of the code should be similar. You can get the array and simply write it as a binary file to the SPIFFS system. You can then play it back again. No need to follow a specific audio format.