Re: std::wstring to glib::ustring conversion



It is much more complicated than I thought since I am new to the topic of character encoding of a string.

For Fabrício's suggestion.  Say thank you for your advice first.  I add one statement to print the ustring after the conversion to the console. 

        std::cout << myustr << std::endl;

The result just got an 'a'.  Maybe it is the wrong way to verify the converted glib::ustring, I'm still unsure.  For one question, why is g_utf16_to_utf8 here?  A sizeof test with wchar_t show wide character (usually) takes 32 bits wide in Unix-like system.  I guess g_utf32_to_utf8 should work but that function doesn't seem to be declared :(


2009/4/28 Fabrício Godoy <skarllot gmail com>
I don't think that Glib::convert can convert (or convert well) wstrings.
My sugestion:

// #include <glib.h>
std::wstring mywstr(L"any text");
gchar* pt_ustr = g_utf16_to_utf8((gunichar2*)mywstr.c_str(), -1, NULL, NULL, NULL);
Glib::ustring myustr(pt_ustr);
g_free(pt_ustr);

References:
1- http://library.gnome.org/devel/glib/stable/glib-Unicode-Manipulation.html#g-utf16-to-utf8

2009/4/28 Milosz Derezynski <internalerror gmail com>

It's not really about the data type used, but about the encoding of the original string.

For one, there is http://www.unicode.org/Public/PROGRAMS/CVTUTF/, which is of course not part of glib, but is probably a good way to do it.

Then, you can use http://gtkmm.org/docs/glibmm-2.4/docs/reference/html/group__CharsetConv.html#g58f7e4556c436a96c64b186bbc8f7f4c , for converting between UTF-16 (which is what I assume your wstrings are), and UTF-8.

Then finally you can place the result into a Glib::ustring.

Hope this helps,

M.

On Tue, Apr 28, 2009 at 12:39 PM, 陈凯 <chenkai036 gmail com> wrote:
Greetings,

Recently I plan to port my app from Windows to linux using Gtkmm library.  I found glib::ustring is recommended to use, but my app works with std::wstring.  However, there seems no sign of documentations about the conversion from std::wstring to glib::string in the gtkmm library.  I got stuck here.  Could some one help me out of the question?  Thanks in advance :)

Regards

CHEN Kai


_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list




--
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list





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