Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino/C:将字节数组转换为字符串或其他类似文本格式_C_Arduino_Bytearray - Fatal编程技术网

Arduino/C:将字节数组转换为字符串或其他类似文本格式

Arduino/C:将字节数组转换为字符串或其他类似文本格式,c,arduino,bytearray,C,Arduino,Bytearray,我正在为一些第三方硬件使用一些第三方库。这些库通过串行连接与硬件通信。使用这些库,我通过串行接口将数据发送到硬件,并获得一个响应,该响应存储在一个数组中: // 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, co

我正在为一些第三方硬件使用一些第三方库。这些库通过串行连接与硬件通信。使用这些库,我通过串行接口将数据发送到硬件,并获得一个响应,该响应存储在一个数组中:

// 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 
我是否需要以某种方式将其转换为字符串,或者与另一个字符数组进行比较?

string.h中的所有函数用于arduino的字符串/内存比较。您可以使用
strcmp
memcmp

注意,在C语言中,不能简单地使用
==
运算符来比较两个字符串。您只需比较两个内存指针的值

以下是缓冲区内的比较示例:

if (strcmp((const char*)gsm.comm_buf, "\r\n+CREG: 0,3\r\n\r\nOK\n")==0)
{
    Serial.print("abc");
}
如果接收到的消息以空字节终止,则可以使用strcmp,否则必须使用memcmp进行作业

对于这两个函数,必须检查返回值是否为零,然后这些字符串相等

如果您不想从缓冲区的第一个字节(索引为零)进行比较,而是想从第五个字节(索引为4)进行比较,则只需将4添加到指针:

if (strcmp((const char*)gsm.comm_buf + 4, "\r\n+CREG: 0,3\r\n\r\nOK\n")==0)

你在找strcmp(com\u buffer,“CREG:0,3”)?
if(strcmp(gsm.comm\u buf,“\r\n+CREG:0,3\r\n\r\nOK\n”){
给出了错误
从“byte*”到“const char*”的无效转换。
是的,你可以通过谷歌搜索错误消息来解决该错误;
strcmp((const char*)*)comm buf,“foobar”)
它与效率无关。在这种(最简单的)情况下,类型转换操作对代码没有任何作用-它只是愚弄了编译器。原因是:
strcmp()
需要两个
const char*
类型的参数,但数组的类型是
byte[]
在传递给函数时会衰减为
字节*
,以及基类型不同的两个指针(
常量字符*
字节*
)是不兼容的类型。非常好的建议,我不知道您链接的引用和==问题。谢谢!
memcmp
需要最后一个参数(…,
size\t num
if (strcmp((const char*)gsm.comm_buf + 4, "\r\n+CREG: 0,3\r\n\r\nOK\n")==0)