One fact that caught my attention is that before I could open the file on the webserver itself, then suddenly I can't open it anymore, I can only download it.
As if the file was made read-protected. Now we see that that initial piece of code is also no longer able to read the file.
Using '\n' gives this error when compiling. pBuffer is uint8_t:
invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]
Is there any test to eliminate this 'dirt'? always generated by the last 20 characters of the base64 string
The code:
myFile = SPIFFS.open("/DATAHORA.txt", "r");
if (myFile) {
//encoding base64
unsigned int fileSize = myFile.size(); // Get the file size.
uint8_t * pBuffer = (uint8_t *) malloc(fileSize); // allocate memory for the file.
if (pBuffer != nullptr) { // if allocation worked
myFile.read(pBuffer, fileSize); // then read the whole file into SRAM.
String toEncode =(char*)pBuffer;
encoded = base64::encode(toEncode);
Serial.println(encoded);
free(pBuffer); // Free the memory that was used by the buffer.
}
myFile.close();
}
When you read the file, you don’t add the null char termination and so when you build the String it read the memory until it finds a zero. If you are unlucky and memory is not null then you get extra characters in the String.
In my code I make room for one extra char and manually use it at the end of the buffer and set it to 0. This way the c-string conversion to a String (what a waste of memory) will work
It's a colossal waste of memory. Especially when you consider there is an overload of the encode() function that doesn't require making a superfluous extra copy of the input data in a String object:
Moreover I wonder about the point of using BASE64 for a text file in the first place. The purpose of BASE64 is to allow representation of binary data as printable ASCII characters. For this ability, it extracts a penalty of 33% expansion in the data size over the original binary. However, that's better than the 100% expansion paid for using ASCII Hex. But, using it for a text file (which is already printable) has me puzzled.