【问题标题】:convert unicodestring to string in xrad studio在 xrad studio 中将 unicodestring 转换为字符串
【发布时间】:2016-01-07 17:27:45
【问题描述】:

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

E2034 无法将“UnicodeString”转换为“字符串”

我的代码如下:

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

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

我的问题如下,我可以在edName 上进行从 unicodestring 到字符串的转换,还是必须更改参数列表的数据类型?如果是这样,我该怎么做?
我一直在寻找这个问题,但没有找到与我的问题相似的任何东西。

【问题讨论】:

  • 我一直在寻找这个问题,但没有找到与我的问题相似的任何东西。 -- ???在字符串类型之间进行转换是这里经常被问到的问题。可能不是 C++ Builder 字符串类型,而是字符串类型(Unicode 到基于 char、基于 char 到 Unicode、基于 char 到基于宽字符、LPCSTR、LPCTSTR 等)。您的 Builder 字符串类型“UnicodeString”可能只是他们说“Unicode”这个词的方式。

标签: c++ string c++builder unicode-string


【解决方案1】:

您需要先将 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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-10
    • 2015-01-09
    • 1970-01-01
    • 2012-07-02
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多