如何把字符串数据转换成uint16_t数据-Arduino中文社区 - Powered by Discuz! Archiver

f85797 发表于 2020-11-21 12:10

如何把字符串数据转换成uint16_t数据

比如我想传个字符串string_data = "{4052,3986,502,2014,500,2014}" 进来

,并且把这个字符串转换成 uint16_t rawdata = {4052,3986,502,2014,500,2014};
应该如何实现呢?

mossan 发表于 2020-12-1 10:54

uint16_t count;
uint16_t *code_array;
String tmp_str;

// Remove the leading "1:1,1," if present.
if (str.startsWith("1:1,1,"))
    tmp_str = str.substring(6);
else
    tmp_str = str;

// Find out how many items there are in the string.
count = countValuesInStr(tmp_str, ',');

// Now we know how many there are, allocate the memory to store them all.
code_array = newCodeArray(count);

// Now convert the strings to integers and place them in code_array.
count = 0;
uint16_t start_from = 0;
int16_t index = -1;
do {
    index = tmp_str.indexOf(',', start_from);
    code_array = tmp_str.substring(start_from, index).toInt();
    start_from = index + 1;
    count++;
} while (index != -1);
截取的代码
页: [1]
查看完整版本: 如何把字符串数据转换成uint16_t数据