我有一个来自 UART 的输入。
uint8_t uartRX_data[UART_RX_BUF_SIZE]="";
我需要将此数据传递给一个函数。而且,在这个函数中,我想将它与预定义的字符串进行比较,例如:
char RESP_OK[] = "OK";
char RESP_ERROR[] = "ERROR";
char RESP_FAIL[] = "FAIL";
最简单的方法是什么?
编辑:我的问题只是关于数据比较和传递给函数的数据。
最佳答案
只要 uartRX_data
中的字符串以 NULL 结尾,您就应该能够像这样使用 strcmp
:
if (strcmp((const char *)uartRX_data, RESP_OK) == 0)
{
// handle OK
}
else if (strcmp((const char *)uartRX_data, RESP_ERROR) == 0)
{
// handle ERROR
}
else if (strcmp((const char *)uartRX_data, RESP_FAIL) == 0)
{
// handle FAIL
}
else
{
// handle unknown response
}
关于c - 如何将 uint8_t 数组与 C 中的字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55967430/