The wxMBConv classes in wxWidgets enable an Unicode-aware application to easily convert between Unicode and the variety of 8-bit encoding systems still in use.
A few years ago, a solution was proposed: the Unicode standard. Able to contain the complete set of characters in use in one unified global coding system, it would resolve the character set problems once and for all.
But it hasn't happened yet, and the migration towards Unicode has created new challenges, resulting in "compatibility encodings" such as UTF-8. A large number of systems out there still depends on the old 8-bit encodings, hampered by the huge amounts of legacy code still widely deployed. Even sending Unicode data from one Unicode-aware system to another may need encoding to an 8-bit multibyte encoding (UTF-7 or UTF-8 is typically used for this purpose), to pass unhindered through any traditional transport channels.
But often, your environment doesn't want Unicode strings. You could be sending data over a network, or processing a text file for some other application. You need a way to quickly convert your easily-handled Unicode data to and from a traditional 8-bit encoding. And this is what the wxMBConv classes do.
A variable, wxConvCurrent, points to the conversion object that the user interface is supposed to use, in the case that the user interface is not Unicode-based (like with GTK+ 1.2). By default, it points to wxConvLibc or wxConvLocal, depending on which works best on the current platform.
The predefined wxCSConv instance, wxConvLocal, is preset to use the default user character set, but you should rarely need to use it directly, it is better to go through wxConvCurrent.
Example 1: Constructing a wxString from input in current encoding.
wxString str(input_data, *wxConvCurrent);
Example 2: Input in UTF-8 encoding.
wxString str(input_data, wxConvUTF8);
Example 3: Input in KOI8-R. Construction of wxCSConv instance on the fly.
Example 4: Printing a wxString to stdout in UTF-8 encoding.
puts(str.mb_str(wxConvUTF8));
Example 5: Printing a wxString to stdout in custom encoding. Using preconstructed wxCSConv instance.
wxCSConv cust(user_encoding); printf("Data: %s\n", (const char*) str.mb_str(cust));
wxString str(wxConvUTF8.cMB2WC(input_data), *wxConvCurrent);
Here, cMB2WC of the UTF8 object returns a wxWCharBuffer containing a Unicode string. The wxString constructor then converts it back to an 8-bit character set using the passed conversion object, *wxConvCurrent. (In a Unicode build of wxWidgets, the constructor ignores the passed conversion object and retains the Unicode data.)
This could also be done by first making a wxString of the original data:
To print a wxChar buffer to a non-Unicode stdout:
printf("Data: %s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));
If you need to do more complex processing on the converted data, you may want to store the temporary buffer in a local variable:
const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(unicode_data); const char *tmp_str = (const char*) tmp_buf; printf("Data: %s\n", tmp_str); process_data(tmp_str);
If a conversion had taken place in cWX2MB (i.e. in a Unicode build), the buffer will be deallocated as soon as tmp_buf goes out of scope. The macro wxWX2MBbuf reflects the correct return value of cWX2MB (either char* or wxCharBuffer), except for the const.
![]() |
[ top ] |