【问题标题】:Converting a substring to integer with toInt [closed]使用 toInt 将子字符串转换为整数 [关闭]
【发布时间】:2019-10-28 23:24:13
【问题描述】:

对于一个 arduino 项目,我想转换一个如下所示的 Ajax 请求:

字符串请求 = "GET /setAlarm&h=21&m=34&end"

我正在尝试将“21”和“34”提取为整数并将它们保存在两个变量中。

substring 方法有效,因为我可以使用

输出正确的信息
Serial.println(request.substring(request.indexOf("&h=") + 3, request.indexOf("&m=")))

到目前为止,我尝试使用.toInt,正如https://stackoverflow.com/a/18200065/12288557 中所建议的那样。我一小时的代码如下所示:

int hour = request.substring(request.indexOf("&h=") + 3, request.indexOf("&m=")).toInt;

使用 .toInt Visual Studio 给我以下错误消息:

错误:无法将 'String::toInt' 从类型 'long int (String::)()const' 转换为类型 'int int hour = request.substring(request.indexOf("&h=") + 3, request.indexOf("&m=")).toInt

【问题讨论】:

    标签: c++ ajax string arduino


    【解决方案1】:

    toInt() 是一个函数,所以你需要在它后面加上括号,因为 C++ 对所有函数都需要这个:

    request.substring(request.indexOf("&h=") + 3, request.indexOf("&m=")).toInt();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 2012-11-04
      • 2012-05-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多