5 c
我在嵌入式系统中使用C语言.我有这个uint8数组.
static uint8_t data_array[20];
Run Code Online (Sandbox Code Playgroud)
data_array的内容以'\n'.结尾.
我想检查前3个字节是否为"ABC".我就是这样做的.
if (data_array[0]=='A' && data_array[1]=='B' && data_array[2]=='C')
{
printf("pattern found\n");
}
Run Code Online (Sandbox Code Playgroud)
是否有更优雅的方法来检测模式?如果模式由10个字节组成,我的方法可能很麻烦.
只需使用一个循环:
static uint8_t data_array[20] = "ABC";
static uint8_t s[4] = "ABC";
static uint8_t length = 3;
uint8_t bool = 1;
for (int i = 0; i < length; i++) {
if (s[i] != data_array[i]) {
bool = 0;
break;
}
}
if (bool) {
printf("pattern found\n");
}
Run Code Online (Sandbox Code Playgroud)