4

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

4
  • You did search before asking, didn't you? Commented Jul 2, 2012 at 10:03
  • Yes im searchin already 1 hour... Commented Jul 2, 2012 at 10:04
  • UnicodeString::t_str Method, yes you can also write into file too, just prefix your file with BOM. Commented Jul 2, 2012 at 10:11
  • 1
    Stay away from the t_str() method. It is dangerous to use, as it modifies the internal data of the UnicodeString, and has been removed in later versions of C++Builder anyway. Commented Jul 2, 2012 at 22:37

3 Answers 3

9

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;
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

us is (ICU) UnicodeString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.