I have a C array like this. (Image 1)
But after I compiled, why is this string automatically appended to my string? (Image 2)
C:/Users/michi/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiAP.cpp
It has broken my HTML content (Image 3)
platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_ldf_mode = deep
lib_deps =
ottowinter/ESPAsyncWebServer-esphome@^3.1.0
adafruit/Adafruit GFX Library@^1.11.9
adafruit/Adafruit SSD1306@^2.5.7
arduino-libraries/NTPClient@^3.2.1
How do I fix it? Thanks!
Sorry because the uploader doesn’t allow me to upload multiple images.
Can you uplaod the project files verbatim?
Your
const char indexHtml[] PROGMEM = {
...
6d, 0x6c, 0x3e};
array is declared to be of const char []
, aka a C-string, but it fails to have a zero-terminator at the end of the array. This is not automatically added in the array.
So, the Serial.println(const char*)
function just prints data starting at the pointer you give it until it encounters a null-byte (the zero terminator). Since you have none in your array, it goes beyond its bounds and prints whatever happens to be after the array until it does hit a zero byte.
Simply add a 0x00
as the last element of the array to make it valid C-string.
Otherewise, you could have also called the Serial.println()
overload that takes a pointer and length, that is
// does not print beyond bounds
Serial.println((const uint8_t*) indexHtml, sizeof(indexHtml));
1 Like
Ah, I just printed it to only for testing.
Btw, I fixed it. Thanks so much!
Also, instead of storing HTML as a hex char array, you should either look into
- using C++ raw strings (
R"()"
) to place the readable HTML in it direclty (Multiline String in C and C++)
- save the verbatim HTML file in
data/
and use the file system capabilities like LittleFS / SPIFFS to serve it. (docs, tutorial))
1 Like