【发布时间】:2013-08-01 18:37:52
【问题描述】:
我正在为一些第三方硬件使用一些第三方库。库通过串行连接与硬件通信。使用库,我通过串行接口向硬件发送数据并获得响应,该响应存储在数组中:
// This is the byte array declared in the third party libraries
// that stores data sent back from the external hardware
byte comm_buf[201];
/* I send data to hardware, comm_buf gets filled */
// Printing out the received data via a second serial line to which
// I have a serial monitor to see the data
for (int i = 0; i <= 50; i++) {
Serial.print(gsm.comm_buf[i]);
}
// This is printed via the second monitoring serial connection (without spaces)
13 10 43 67 82 69 71 58 32 48 44 51 13 10 13 10 79 75 13 10 00
// It is the decimal ascii codes for the following text
+CREG: 0,3
如何将字节数组转换为我可以在代码中评估的格式,以便我可以执行类似以下伪代码的操作;
byte comm_buf[201];
/* I send data to hardware, comm_buf gets filled */
if (comm_buf[] == "CREG: 0,3" ) {
// do stuff here
}
我是否需要以某种方式将其转换为字符串,或者与另一个 char 数组进行比较?
【问题讨论】:
-
您在寻找
strcmp(com_buffer, "CREG: 0,3")吗? -
if (strcmp(gsm.comm_buf,"\r\n+CREG: 0,3\r\n\r\nOK\n")) {给出错误invalid conversion from 'byte*' to 'const char*' -
是的,您可以通过谷歌搜索错误消息来解决该错误;)
-
strcmp((const char *)comm_buf, "foobar") -
与效率无关。在这种(最简单的)情况下,类型转换操作在代码中什么都不做——它只会愚弄编译器。原因:
strcmp()需要两个const char *类型的参数,但您的数组是byte[]类型,当传递给函数时会衰减为byte *- 以及两个基本类型不同的指针 (const char *和byte *在你的情况下)是不兼容的类型。