Arduino加入字符串和*char

Jur*_*ure 5 arduino concatenation arduino-ide arduino-uno

我是arduino的新手,我偶然发现了一个问题.我想通过我的esp8266发送数据到我的php页面.但我不知道如何使用此GET请求加入我的数据.

这是我的代码:

String card = "2-3d-fg-d6-12-68-32-3f-35-45-42-53-2a-3";
char *hello = "GET /insert.php?card="+card+"&error=1 HTTP/1.1\r\nHost: testsite.com\r\n\r\n"; 
wifi.send((const uint8_t*)hello, strlen(hello));
Run Code Online (Sandbox Code Playgroud)

这就是我在arduino控制台中得到的:

错误:初始化时无法将'StringSumHelper'转换为'char*',无法在初始化时将'StringSumHelper'转换为'char*'

小智 6

你可以使用std::string::c_str()function,它返回一个指向const char缓冲区的指针:

String card = "2-3d-fg-d6-12-68-32-3f-35-45-42-53-2a-3";
char *prefix = "GET /insert.php?card=";
char *postfix ="&error=1 HTTP/1.1\r\nHost: testsite.com\r\n\r\n"; 

String url = prefix +card+ postfix;
const char *url_complete = url.c_str();
//...
Run Code Online (Sandbox Code Playgroud)

另请参阅相关文章:如何连接字符串和const char?