在xradstudio中将unicodestring转换为字符串



我在"Rad Studio 10 Seattle"中遇到问题,我试图从TEdit对象获取文本输入,但收到一条错误消息,说

E2034无法将"UnicodeString"转换为"string"

我的代码如下:

this->shopVar->addDog(edName->Text, StrToInt(edAge->Text), "male", "dogspecial");

此函数addDog采用(string, int, string, string)
当我尝试用edName->TextTEdit对象发送文本时,我会收到前面提到的错误消息。

我的问题如下,我可以在edName上从unicodestring转换为string吗?还是必须更改参数列表的数据类型?如果是,我该怎么做
我一直在寻找这个问题,但没有发现任何与我的问题类似的东西。

您需要先将UnicodeString数据转换为Ansi,然后再传递。最简单的方法是先将UnicodeString分配给AnsiString(或导数),然后使用其c_str()方法获得char*指针,std::string将接受该指针:

this->shopVar->addDog(AnsiString(edName->Text).c_str(), ...);

如果可能,可以考虑重写shopVar类以使用std::wstring,然后可以删除Ansi转换:

this->shopVar->addDog(edName->Text.c_str(), StrToInt(edAge->Text), L"male", L"dogspecial");

最新更新