我已经尝试解决了半天...没有成功...
我有一个结构:
typedef struct s_iomodus {
const char* SENSOR;
const char* POSITION_1;
const char* SHOW_MI;
const char* POSITION_2;
const char* TYPE_1;
const char* TYPE_2;
const char* DESCRIPTION; // LOC Description of the
const int NRVALUES;
} iomodus_t;
iomodus_t iomodus[] = {
{ "Relay","WW_Tank","WW_Pumpe_An_Aus","NO_P2","NO_T1","NO_T2" ,"MAGNETIC", 1}, //D25 :
{ "Relay","Puffer_Tank","NO_SHOW","NO_P2","NO_T1","NO_T2" ,"SSR", 1}, //D26 :
{ "Relay","WW_Tank","Valve","Auslauf_unten","Zu","Auf" ,"MAGNETIC", 2}, //D27 :
我在其中存储所有PINS和位置的所有STATUS QUO设置,并将其作为主题发送到MQTT服务器。
现在我必须从这个数组构建一个字符串,以VOID SETUP的形式发送它
void setup()
{
Serial.begin(115200);
MQTTclient.setServer(server, 1883);
MQTTclient.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
const char *c_topic = concat_strings(iomodus[i].POSITION_1,iomodus[i].SENSOR, iomodus[i].POSITION_2);
Serial.println(c_topic);
MQTTclient.publish(c_topic, iomodus[i].VERSION);
}
我的问题是...该函数无法正确生成字符串
在两者之间不加“/”!
应该是这样的字符串:
“MQTTTOPIC_PREFIX / TEXT_str1 / TEXT_str2 / TEXT_str3”
要么
如果“str4”存在/不为空,则为“MQTTTOPIC_PREFIX / TEXT_str1 / TEXT_str2 / TEXT_str3 / TEXT_str4”
const char *concat_strings(const char *str1, const char *str2, const char *str3,const char *str4)
{
// define a buffer
static char result[MAX_CONCAT_LEN] = {0};
// counter part
int i = 0;
const char *slash = {"/"};
const char *PREF= {MQTTTOPIC_PREFIX};
size_t len = strlen(PREF)+strlen(str1)+strlen(str2)+strlen(str3);
// loop until end of ID has reached or destination buffer is full
while(*PREF && i < MAX_CONCAT_LEN)
{result[i++] = *PREF++;}
// loop until end of string 1 has reached or destination buffer is full
while(*str1 && i < MAX_CONCAT_LEN)
{result[i++] = *str1++;}
// loop until end of string 2 has reached or destination buffer is full
while(*str2 && i < MAX_CONCAT_LEN)
{result[i++] = *str2++;}
if (str3==TRUE){
// loop until end of SLASH has reached or destination buffer is full
while(*slash && i < MAX_CONCAT_LEN)
{result[i++] = *slash++;}
while(*str3 && i < MAX_CONCAT_LEN)
{ result[i++] = *str3++;}
}
if (str4==TRUE){
// loop until end of SLASH has reached or destination buffer is full
while(*slash && i < MAX_CONCAT_LEN)
{result[i++] = *slash++;}
while(*str4 && i < MAX_CONCAT_LEN)
{ result[i++] = *str4++;}
}
result[len+1] = 0;
return result;
}
因此,即使我这样做,也不会添加“/”并且也不想识别,如果str3或str4为FALSE ...
请问,如果可用str3或str4(str1和str2始终存在),最简单的方式加入数组并添加“/”
先感谢您!
最佳答案
代码中有几个问题:
result
:static char result[MAX_CONCAT_LEN] = {0};
str4
的默认值声明该函数:const char *concat_strings(const char *str1, const char *str2, const char *str3,const char *str4 = NULL);
char*
与TRUE
进行比较,而应使用NULL
代替:if (str4 != NULL){
PREF
,str1
和str2
之间,没有添加分隔符的代码:while(*PREF && i < MAX_CONCAT_LEN)
{result[i++] = *PREF++;}
// loop until end of string 1 has reached or destination buffer is full
while(*str1 && i < MAX_CONCAT_LEN)
{result[i++] = *str1++;}
// loop until end of string 2 has reached or destination buffer is full
while(*str2 && i < MAX_CONCAT_LEN)
{result[i++] = *str2++;}
str4
中,由于以下原因,slash
指向字符串的末尾: while(*slash && i < MAX_CONCAT_LEN)
{result[i++] = *slash++;}
在上一个循环中。 (除非分隔符将来可能超过一个字符,否则您通常不需要在此处循环)
len
时,不调整str4
:size_t len = strlen(PREF)+strlen(str1)+strlen(str2)+strlen(str3);
result[len+1] = 0;
仍然会切断
str4
。 如果可能,最好使用
String
class或 strcat()
。有关使用Arduino
String
类进行字符串连接的示例,请参见here,here和here。 // adding a constant integer to a string:
stringThree = stringOne + 123;
// adding a constant long interger to a string:
stringThree = stringOne + 123456789;
// adding a constant character to a string:
stringThree = stringOne + 'A';
// adding a constant string to a string:
stringThree = stringOne + "abc";
// adding two Strings together:
stringThree = stringOne + stringTwo;
关于c++ - 连接Char数组的函数,包括之间的其他符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34770031/