C++ convert UnicodeString into String

孤人 提交于 2020-01-04 15:57:50

问题


Please help me somebody to convert unicodestring into string

This is how i am getting unicodestring

UnicodeString _str = OpenDialog1->FileName;

Or if it possible to write into file unicode string with ifstream or something like that?

Thanks


回答1:


Depending on your needs, assign the UnicodeString to an AnsiString or a UTF8String, and then write that to your file instead of the original UnicodeString itself:

UnicodeString _str = OpenDialog1->FileName; 
AnsiString _astr = _str;

Or:

UnicodeString _str = OpenDialog1->FileName; 
UTF8String _ustr = _str;

To pass an AnsiString/UTF8String to an STL function, you have to either:

1) use the c_str() method:

stream << _astr.c_str();

2) construct a temp std::string:

stream << std::string(_astr.c_str(), _astr.Length());

3) in the case of AnsiString only, specify the VCL_IOSTREAM define in your project to enable AnsiString's own <<< and >> operators:

stream << _astr;



回答2:


Converting your string to bytes would require some encoding. There are various libraries that do this, so it depends on the framework you are using.

As an alternative, you could use wofstream to write wchar_t characters to the stream.




回答3:


std::string converted;
us.toUTF8String(converted);

us is (ICU) UnicodeString



来源:https://stackoverflow.com/questions/11291397/c-convert-unicodestring-into-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!