I am using a function which accepts String objects as parameters. This function is used to send out data through the MQTT protocol. I need to send binary data through MQTT. I managed to do that but there is a problem with my solution. Here is the relevant code;
MqttClient mqtt("192.168.1.71", 1883, onMessageReceived);
char buffer[100];
//buffer filled with binary data terminated with null character at this point
mqtt_msg = String(buffer);
String topic = "XXX";
mqtt.publish(topic, mqtt_msg );
I will terminate the binary data in buffer with a null character 0x00. The problem comes when 0x00 happened to be part of the binary data. How can this problem be solved?
The String object is similar to the String object used in Arduino.
Stringclass has a constructor that takes both aconst char*and a (hopefullysize_t) length/size value. That's the waystd::stringallows you to construct it with arbitrary binary data of a specific length, possibly including NULs, without any need for the buffer you're copying data from to be NUL terminated.publishmethod that takes a pointer to the payload and the length. (And that particular method may require other parameters such as QoS, etc.)