在我之前的代码中,我使用以下代码行来获取"命令"的最后9位数。串
if(command.indexOf("kitchen light: set top color") >=0)
{OnColorValueRed = (command.charAt(28)- 48)*100 + (command.charAt(29)- 48)*10 + (command.charAt(30)- 48);}
现在我正在使用char缓冲区(char packetBuffer [UDP_TX_PACKET_MAX_SIZE];)并且使用上面的代码不起作用,因为packetBuffer不是字符串,我怎么能请这个呢?
答案 0 :(得分:0)
尝试定义一个搜索字符串的函数
int indexOf_for_char(const char *str, int str_length, const char *target) {
// naive method
for (int index = 0; index < str_length; index++) {
int j;
// check if matched
for (j = 0; target[j] != '\0' && index + j < str_length && str[index + j] == target[j]; j++);
// if matched, return the index
if (target[j] == '\0') return index;
}
return -1;
}
并使用下标。
if(indexOf_for_char(packetBuffer, UDP_TX_PACKET_MAX_SIZE, "kitchen light: set top color") >=0)
{OnColorValueRed = (packetBuffer[28]- 48)*100 + (packetBuffer[29]- 48)*10 + (packetBuffer[30]- 48);}