c++ - 将 unicode 字符串转换为字符串

标签 c++ string class unicode typeconverter

我在 C++ Builder 中创建了一些 C++ 类。我现在在 VCL 表单应用程序中使用这些。我有一个加载文本文件并将字符串作为参数的函数。

我正在使用 openDialog 控件浏览到文件,然后将其打开。

我的问题是:openFialog 的 .filename 属性是 UnicodeString 的形式,我的函数需要一个 std::string。如何将 unicode 字符串转换为 std::string?

这是我的代码:

OpenDialog1->Execute();
calCalendar.loadAppointmentsFromFile(OpenDialog1->FileName.t_str());

函数定义如下:

void loadAppointmentsFromFile(const std::string& stringFilename);

我收到以下错误:

[BCC32 错误] Assessment2.cpp(39): E2342 参数“stringFilename”中的类型不匹配(需要“const std::string &”,得到“wchar_t *”)

我可以帮忙解决这个问题吗?

最佳答案

使用UnicodeString::t_str得到一个缩小的字符串。但是,您应该考虑不要将两者混用。

另一种选择是首先转换为 AnsiString:

AnsiString str = OpenDialog1->FileName;
std::string s(str.c_str());
loadAppointmentsFromFile(s);

或者

std::string s = OpenDialog1->FileName.t_str(); // TCHAR mapping set to char
loadAppointmentsFromFile(s);

关于c++ - 将 unicode 字符串转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12558635/

相关文章:

c++ - 为什么可以修改数组 b?

c++ - Depth + Stencil 帧缓冲区问题

Java 的 String.replaceAll 不起作用

c - C中使用getchar存储用户输入的多个字符

c - strcat 导致字符串变空

c++ - 空类和派生虚拟类的大小

ios - 从 Swift 中的类实例获取类型

c++ - 使用 scons 构建自定义文件

c++ - 无法在 XCode 4 项目中嵌入 C++ 中安装 libjson

java - 在Java中的不同类中使用函数,而不将其作为方法收集在类中