Output wxString as UTF8

Milan Babuskov milanb at panonnet.net
Sat Sep 30 00:41:13 PDT 2006


Vadim Zeitlin wrote:
> No because the result of conversion to UTF-8 is not a wxString (at least
> not in Unicode build), it's a wxCharBuffer.

I found this on one website:

void wxUTF8OutputStream::Write(const wxString& outputString)
{
   wxOutputStream os;
   ...
   #if wxUSE_UNICODE
     const wxWX2MBbuf buf(outputString.mb_str(wxConvUTF8));
     os.Write((const char*)buf, strlen((const char*)buf));
   #else
     wxString str2(outputString.wc_str(*wxConvCurrent), wxConvUTF8);
     os.Write(str2.mb_str(), str2.Len());
   #endif
}

Since I'm going to write to UTF8 file anyway, I think I'd use something 
like this. Is this correct?


 > And, of course, you probably want to handle the errors of cMB2WC()
 > call in some other way than just returning an empty string.

What kind of errors should I expect? Shouldn't any string be convertible 
to Unicode?

Thanks,

-- 
Milan Babuskov
http://swoes.blogspot.com/
http://www.flamerobin.org





More information about the wx-users mailing list