0

在我以前的代码中,我使用以下代码行来获取“命令”字符串的最后 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 packetBuffer[UDP_TX_PACKET_MAX_SIZE];)并且使用上面的代码不起作用,因为 packetBuffer 不是一个字符串,我该如何解决这个问题

4

1 回答 1

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);}
于 2015-09-30T14:43:57.773 回答