Re: Glib::ustring in utf8 to std::wstring



On Wed, 20 May 2009 10:42:17 +0100
Filipe Apostolo <f apostolo cbr gmail com> wrote:

> 
> Hi to all,
> 
> I'm facing a problem that consists to go back to the previous
> encodding. I'm workin in windows and with DCOM objects. Those DCOM
> Objects works with wchar parameters.
> 
> So if I' want to list in a Gtk::TreeView the names of the available
> DCOM servers (for example) I'Need to convert the names from
> std::wstring to Glib::ustring  and convert to utf8 to show characters
> like 'ç', ã, .. For this task I do it like this:
> *********
> std::wstring origin = source_str;
> std::string equivalentStr;
> equivalentStr.assign(origin_wstr.begin(), origin_wstr.end());
> Glib::ustring ustr= Glib::locale_to_utf8 (origin);
> 
> Then ..
> row[mColumModel.name] = ustr;
> 
> *********
> with this steps If I heave a Name like 'Renovação'
> In the treeview It apears has it should.
> But when user select this row and I wan't to convert it again to
> wstring It trims the string like 'Renova';
> 
> The back convertion I do it in this way:
> 
> *************
> std::string str = Glib::locale_from_utf8 (ustr);
> std::wstring convertedWStr (str.length(),L' ');
> std::copy(str.begin(),
>               str.end(),
>               convertedWStr.begin());
> *************

Neither of your approaches will work reliably.  Your attempt
to convert between narrow to wide codesets via iterators causes
your c++ implementation to use the narrow() and widen() members of
the ctype facet for the your current locale to achieve the conversion.
This will only work with fixed width character sets.  In particular, it
will not work with other than ASCII characters if the narrow character
set of your current locale uses a multibyte character set such as
UTF-8.  (In fact, it appears that the widen() member is also limiting
itself to ASCII input even though your locale must in fact use a fixed
width narrow character set such as Latin-1 for the narrow to wide
conversion to have worked.)

You should note also that if you are using windows, not even the wide
character set is of fixed size as you appear to assume,  (It uses
UTF-16.)

If you want to hard-code to windows, I suggest you use
g_utf8_to_utf16() and g_utf16_to_utf8(), from glib.

Chris



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]