wxRichTextCtrl and unicode / utf-8 Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
vouk
Knows some wx things
Knows some wx things
Posts: 25
Joined: Fri Feb 15, 2019 7:34 am
Location: Germany
Contact:

wxRichTextCtrl and unicode / utf-8

Post by vouk »

I am using wxWidgets 3.1.2 on Windows with MSVC. I have this code:

Code: Select all

richText.WriteText("Valnir\u00c6sirsson"); // Got this from external JSON data
Unfortunately it appears as:

Code: Select all

ValnirÆsirsson
It should be:

Code: Select all

ValnirÆsirsson
How can I fix this? Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxRichTextCtrl and unicode / utf-8

Post by PB »

(I assume that in your code you pass a variable and not a literal stored in a source file)

I guess that you need to convert it to the encoding wxString uses (e.g., UTF-16 on MSW):

Code: Select all

richText.WriteText(wxString::FromUTF8("Valnir\u00c6sirsson")); 
Checking if the conversion succeeded (i.e., wxString has the same number of characters as the UTF-8 string) could be a good idea.
User avatar
vouk
Knows some wx things
Knows some wx things
Posts: 25
Joined: Fri Feb 15, 2019 7:34 am
Location: Germany
Contact:

Re: wxRichTextCtrl and unicode / utf-8

Post by vouk »

Sorry, i was not precise enough.

The value is coming from external JSON:

Code: Select all

[{"name":"Valnir\u00c6sirsson"}]
I read that in with wxHTTP and it is stored in a wxString like this:

Code: Select all

res = {m_impl=L"[{\"name\":\"Valnir\\u00c6sirsson\"}]"}
So unfortunately it is an actual backslash in there. So it seems I need to rather fix that JSON endpoint.
Post Reply