将 Delphi 的字符串转换为 std::字符串用于C++
Converting String for Delphi to std::string for C++
我正在开发一个设备(ESP32),有一个rxValue变量定义如下;
std::string rxValue = pCharacteristic->getValue(); <- ESP32 (C++) side
但是我需要通过SetValueAs从Delphi应用程序发送数据(字符串或字符)...方法,但是当我使用
Characteristics.SetValueAsString('b'); <- Delphi side (but problematic)
编辑:我在这里添加SetValueAsString过程
procedure TBluetoothGattCharacteristic.SetValueAsString(const AValue: string; IsUTF8: Boolean);
begin
if IsUTF8 then
Value := TEncoding.UTF8.GetBytes(AValue)
else
Value := TEncoding.Unicode.GetBytes(AValue);
end;
Arduino串行监视器向我显示一个奇怪的字符(方形),命令无法执行。
for (int i = 0; i < rxValue.length(); i++) { <- ESP32 (C++) side
Serial.print(rxValue[i]);
}
if (rxValue.find("a") != -1) {
digitalWrite(LED, HIGH);
}
因此,将字符串从德尔福发送到C++(ESP32 板)作为std::string的方式是什么?另外(我不想单独问一个小问题。将 rxValue 与字符串精确比较的方法是什么?我的意思是,如果rxValue与abcdef执行命令完全相同? 谢谢现在..
深入研究后,问题变得更加复杂。正如@RemyLebeau指出的那样,主要问题是 8 位/16 位字符串不兼容,@David Heffernan 是正确的,因为您不能使用 be use std::string 进行跨编程语言的互操作。
所以我决定将我的数据转换为8Bit AnsiString并发送,但这次我看到Delphi不再支持移动编译器中的AnsiChar和Ansistring(你可以在这里和这里阅读)。
所以我不能在 Delphi 移动中使用 8 位字符串(有很多人他们问 Embarcadero"为什么",因为这意味着你不能使用使用 8 位的设备(许多物联网设备)),但谢天谢地,有一些好人用自定义库解决了这个问题(你可以在这里找到源代码)。
将此库添加到我的应用程序并执行如下命令后,问题已解决;
Characteristics.SetValueAsString(RawByteString('b'));
但答案是:你不能这样使用 std::string,它不适合互操作。
相关文章:
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 组合字符串不适用于 libCurl,C++
- 重载 std::字符串运算符+ 用于打印枚举名称
- 用于子集字符串的 Rcpp 函数
- C++ 虽然语句不适用于字符串和"or"
- C++ 用于在数组中打印字符串的随机数
- 用于拆分空格字符串的程序不起作用
- 用于从 ANSI 字符串转换为 std::basic_string <TCHAR>的正确函数声明
- C ++ txt 文件成数组逐行字符串和 int 用于纸牌游戏
- 为什么我的 if 语句不适用于 c++ 中的字符串?
- C++自定义流操纵器,用于更改流上的下一个字符串
- 如何在 python 包装中使用 unicode 字符串用于带有 cython 的 c++ 类?
- 将 Delphi 的字符串转换为 std::字符串用于C++
- C :将文本文件的内容存储在2D数组中,作为字符串(用于NULL终结器的麻烦?)
- 如何在 tchar 中添加格式化字符串(用于多个文件名等)
- Google协议缓冲区和std::字符串用于任意二进制数据