简体   繁体   English

如何在C ++(Arduino / C ++)中将字符串转换为uint8_t?

[英]How can I convert a String to a uint8_t in C++ (Arduino/C++)?

I have a string: String outputValue = "" that I then append to to build a JSON-like structure to send to a remote device. 我有一个字符串: String outputValue = "" ,然后将其附加到其中以构建一个类似于JSON的结构以发送到远程设备。 This code runs on an ATMEGA328P using the Arduino bootloader. 该代码使用Arduino引导程序在ATMEGA328P上运行。 I append values by doing outputValue += "hello" . 我通过执行outputValue += "hello"

The library I am using to send the values needs a uint8_t * for it's payload with the associated length of that payload. 我用来发送值的库需要一个uint8_t *来作为其有效负载以及相关的有效负载长度。 Is there any way to cast/convert this String to a uint8_t array or is there a preferred way to do a builder rather than using String? 有什么方法可以将此String强制转换/转换为uint8_t数组,还是有一种首选的生成方法而不是使用String? I'm open to any suggestions 我愿意接受任何建议

My working code that I used to test the library has the following. 我用来测试库的工作代码如下。 Note, this is just the result of me copying the raw outputValue to Notepad, enclosing each character in single quotes, then hardcoding this new array to test: 注意,这只是我将原始outputValue复制到Notepad,将每个字符括在单引号中,然后对该新数组进行硬编码以进行测试的结果:

uint8_t testCmd[] = { '{', '"', 'T', '"', ':', '"', 'A', '1', '"', ',', '"', 'E', '"', ':', '-', '1', ',', '"', 'I', '"', ':', '[', '[', '1', ',', '[', '[', '[', '[', '2', '5', '0', ',', '2', ']', ',', '[', '0', ',', '4', ']', ']', ']', ']', ']', ']', '}' };

ZBTxRequest txReq = ZBTxRequest(coordinatorAddr64, testCmd, sizeof(testCmd));   
ZBTxStatusResponse txResp = ZBTxStatusResponse();

xbee.send(txReq);

Yes there is. 就在这里。

Do something like this: 做这样的事情:

String testString = "Test String";
uint8_t* pointer = (uint8_t*)testString.c_str();
int length = testString.length();

EDIT: 编辑:

You should apply it to your problem as follows: 您应该将其应用于您的问题,如下所示:

String testString = "Test String";
uint8_t* pointer = (uint8_t*)testString.c_str();
int length = testString.length();

ZBTxRequest txReq = ZBTxRequest(coordinatorAddr64, pointer, length);   
ZBTxStatusResponse txResp = ZBTxStatusResponse();

xbee.send(txReq);

You can write your array like this: 您可以这样编写数组:

uint8_t testCmd[] = R"({"T":"A1","E":-1,"I":[[1,[[[[250,2],[0,4]]]]]]})";

The difference is that it will have 48 elements instead of 47 like your original one, because of terminating 0. Since you are providing the length of packet you could -1 it: 不同之处在于,由于终止0,它将具有48个元素,而不是原始元素那样的47个元素。由于要提供数据包的长度,因此可以将其设为-1:

ZBTxRequest txReq = ZBTxRequest(coordinatorAddr64, testCmd, sizeof(testCmd) - 1);   
ZBTxStatusResponse txResp = ZBTxStatusResponse();

xbee.send(txReq);

Looking at Arduino reference. 查看Arduino参考。 String object has c_str() method as well as length() . String对象具有c_str()方法以及length() So you can simply try: 因此,您可以尝试:

String testCmd R"({"T":"A1","E":-1,"I":[[1,[[[[250,2],[0,4]]]]]]})";

ZBTxRequest txReq = ZBTxRequest(coordinatorAddr64, (uint8_t *)testCmd.c_str(), (uint8_t)testCmd.length());   
ZBTxStatusResponse txResp = ZBTxStatusResponse();

xbee.send(txReq);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM