Converting strings

Once you have chosen which object you want to use to convert your text, here is how you would use them with wxString. These examples all assume that you are using a Unicode build of wxWidgets, although they will still compile in a non-Unicode build (they just won't convert anything).

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.

wxString str(input_data, wxCSConv(wxT("koi8-r")));

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));

Note: Since mb_str() returns a temporary wxCharBuffer to hold the result of the conversion, you need to explicitly cast it to const char* if you use it in a vararg context (like with printf).

ymasuda 平成17年11月19日