【发布时间】:2020-03-22 17:59:40
【问题描述】:
感谢您的时间,所以我有一个来自 mqtt 的 Char*
我想将其分解为 3 个单独的值
Char* mqttvalue
//Input Would be like the below for example.
mqttvalue = (255,200,230);
// I would like to split the values into the below.
int 1 = 255
int 2 = 200
int 3 = 230
我试过 strtok 没有运气。可能真的很愚蠢,但一些指导会有所帮助。
谢谢
编辑,我试过了。
//Dummy Value for testing
Split("255,240,230");
//Split Value
void Split(char* e) {
String v[3];
char *p;
int i = 0;
p = strtok(e, ",");
while(p && i < 3)
{
v[i] = p;
p = strtok(NULL, ",");
Serial.println(p);
++i;
};
Serial.println(v[0]);
Serial.println(v[1]);
Serial.println(v[2]);
}
【问题讨论】:
-
然后选择 C 或 C++。每种语言使用不同的函数。
-
查看
strtok文档后,您的方法似乎很好。你有什么问题?你得到什么输出? “没有运气”不是很具有描述性 -
你调用
split并带有一个字符串。这不是strtok的有效参数,因为它修改了提供的字符串。 -
你上面的伪代码没有任何意义。您谈论拆分字符串,但使用整数来存储结果。此外,括号中的某些整数与输入字符串不同。在准备问题时,您应该更加精确。
-
我认为关键可能在于@Gerhardh 所说的。像
char test[] = "255,240,230";一样创建一个测试char*并将that 传递给Split。