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

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1999|回复: 1

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

[复制链接]
发表于 2020-11-21 12:10 | 显示全部楼层 |阅读模式
比如我想传个字符串string_data = "{4052,3986,502,2014,500,2014}" 进来

,并且把这个字符串转换成 uint16_t rawdata[6] = {4052,3986,502,2014,500,2014};
应该如何实现呢?
发表于 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[count] = tmp_str.substring(start_from, index).toInt();
    start_from = index + 1;
    count++;
  } while (index != -1);
截取的代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 10:40 , Processed in 0.134420 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表